变量赋值后保持为空

时间:2019-12-23 16:32:49

标签: c# unity3d

我正在使用Initialize方法分配targetparent。当我尝试在Update或moveToTarget中使用它们时,它们似乎为null。每次我调用Initialiaze方法时,它们都会得到一个值,因为debug.log显示其名称。给它们赋值后,我不知道它们会变成空。

    private FollowPath target;

    private FireEnemy parent;
    void Update()
    {
        Debug.Log(target);
        moveToTarget();
    }
    public void Initialize(FireEnemy givenParent)
    {

        this.target = givenParent.Target;
        this.parent = givenParent;
        Debug.Log(parent);
        Debug.Log(target);
    }
    private void moveToTarget()
    {
        if (target != null && target.enabled)
        {
            transform.position = Vector3.MoveTowards(transform.position, target.transform.position,
                Time.deltaTime * parent.ProjectileSpeed);
            Debug.Log(target+ "MovingTowardsTarget");

        }
    }

在这里调用FireEnemy脚本中的Initialize

private void Shoot()
    {
        Projectile projectile = projectileType.GetComponent<Projectile>();
        Instantiate(projectile, transform.position, Quaternion.identity);
        projectile.Initialize(this);
        Debug.Log("bullet initiliazed");

    }

0 个答案:

没有答案