【问题标题】:Trying to access variable from other object试图从其他对象访问变量
【发布时间】:2020-08-23 03:35:32
【问题描述】:

当玩家杀死敌人时,我需要给他弹药。我正在尝试使用 GetComponent,但我不断收到错误“:错误 CS0428:无法将方法组 'GetComponent' 转换为非委托类型 'thro'。您打算调用该方法吗?”在这一行:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class collision : MonoBehaviour
{
 public int cardno = 13;
 public GameObject deck;
 thro deck_script;

 void OnCollisionEnter (Collision cl)
 {
  if(cl.collider.tag == "iscard")
  {
   int cardran = Random.Range(1,14);
   Debug.Log(cardran);
   *deck_script = deck.GetComponent<thro>;*
   if (cardran == cardno)
   {
    
    Destroy(gameObject);
    Debug.Log("kil");
   }
  }
 }
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:
    deck_script = deck.GetComponent<thro>;
    

    调用函数的语法不正确。试试:

    deck_script = deck.GetComponent<thro>();
    

    查看文档:https://docs.unity3d.com/ScriptReference/Component.GetComponent.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-09
      • 2016-07-15
      • 2015-10-12
      • 2018-03-07
      • 2015-07-30
      • 2016-10-25
      • 2012-08-20
      • 2017-09-15
      相关资源
      最近更新 更多