【问题标题】:Object reference not set to instance of an object Unity3D c#对象引用未设置为对象 Unity3D c# 的实例
【发布时间】:2014-07-27 17:48:42
【问题描述】:

这是播放器脚本:

public class player
{      public projectile pro;
       pro = GetComponent<projectile>();
    void Update()
    {
        GameObject go = GameObject.Find("enemy");
        Transform playerTransform = go.transform;
        Vector3 posi = playerTransform.position;
        pro.Target = posi; // getting error here
        Instantiate(bulletprefab, position, Quaternion.identity);   
    }
}

这是射弹脚本: 要求:获取 Projectile 中目标的更新位置,但是由于从 Start() 调用了 projectileMotion() 方法,并且我希望在 Player 中调用 Instantiate 时目标当前的位置,以便适当地计算 Target_Distance在 Projectile 类中, 但看起来从 Vector3 到 _pro.Target 的分配似乎是不允许的。我可以解决这个问题吗?

 public class projectile : MonoBehaviour 
{
    public Vector3 Target;
    public GameObject bulletprefab;
        public float firingAngle = 45.0f;
        public float gravity = 9.8f;
       public Transform Projectile;      
        private Transform myTransform;

        void Awake()
        {
            myTransform = transform;  

        }

        void Start()
    {    myTransform.LookAt(Target);
        StartCoroutine (ProjectileMotion ());

        }



        IEnumerator ProjectileMotion()
        {

            yield return new WaitForSeconds(0.25f);


            Projectile.position = myTransform.position + new Vector3(0, 0.0f, 0);

            // Calculating distance to target
        float target_Distance = Vector3.Distance(Projectile.position, Target );
        Debug.Log ("realUPDATEDOR not" + Target);

            float projectile_Velocity = target_Distance / (Mathf.Sin(2 * firingAngle* Mathf.Deg2Rad) / gravity);

            // X  Y componenent of the velocity
            float Vx = Mathf.Sqrt(projectile_Velocity) * Mathf.Cos(firingAngle * Mathf.Deg2Rad);
            float Vy = Mathf.Sqrt(projectile_Velocity) * Mathf.Sin(firingAngle * Mathf.Deg2Rad);

            // flight time since depends on horizontal component of velocity
            float flightDuration = target_Distance / Vx;

            // projectile rotated at target
            Projectile.rotation = Quaternion.LookRotation(Target - Projectile.position);

            float elapse_time = 0;

            while (elapse_time < flightDuration)               //looping and incrementing elapsed time
            {
                Projectile.Translate(0, (Vy - (gravity * elapse_time)) * Time.deltaTime, Vx * Time.deltaTime);

                elapse_time += Time.deltaTime;

                yield return null;
            }
        }  
    }

Target is present is Projectile class and is only Vector3,如何解决这个错误?

【问题讨论】:

  • pro = new projectile; 之类的东西在哪里?
  • 团结一致:我们用GetComponent,我用过
  • projectile 是脚本吗?如果是这样,你也可以显示它的代码吗?谢谢
  • 当然。更新@扎克
  • 谢谢,我现在去看看

标签: c# unity3d


【解决方案1】:

引用“射弹”脚本的代码不起作用。

改变:

public projectile pro;    
pro = GetComponent<projectile>();

到这样的事情:

public projectile pro;
void Start()
{
pro = GetComponent<projectile>();
}

另外作为一个建议,您可能想减少 GameObject.Find 在更新函数中的使用,因为它的成本很高。

【讨论】:

  • 是的,我明白了,但请问有什么替代方案吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多