如何在没有Public GameObject的情况下禁用另一个脚本?

时间:2018-04-19 16:37:14

标签: unity3d

我只想禁用播放器管理器中的脚本。但是因为我的敌人通过Set Active产生,公共游戏对象才起作用。我试过让我的播放器管理器也是一个预制件,但没有解决方案。我可以通过代码行通过脚本中的特定GameObject手动指定它吗?

虽然这似乎很小,但我确实做过研究,大多数都指向专门使用public GameObject。除非我没有很好地搜索C#。

public float lookRadius = 40f;

//public Casting stop;

Transform target;
UnityEngine.AI.NavMeshAgent agent;

Rigidbody theRigidBody;

void Start(){
    target = PlayerManager.instance.player.transform;
    agent = GetComponent<UnityEngine.AI.NavMeshAgent>();
}


void Update(){
    float distance = Vector3.Distance (target.position, transform.position);
    if (distance <= lookRadius)
    {
        agent.SetDestination (target.position);

        if (distance <= agent.stoppingDistance)
        {
            FaceTarget ();
        }

        if (distance < 10f) // or some distance
        {
            //gameObject.GetComponent<Casting>().enabled = false;
            Debug.Log("nearby heyy");
        }
    }

}

void FaceTarget()
{
    Vector3 direction = (target.position - transform.position).normalized;
    Quaternion lookRotation = Quaternion.LookRotation (new Vector3 (direction.x, 0, direction.z));
    transform.rotation = Quaternion.Slerp (transform.rotation, lookRotation, Time.deltaTime * 5f);
}


// Use this for initialization
public void OnObjectSpawn () {


    //myRender = GetComponent<Renderer> ();
    theRigidBody = GetComponent<Rigidbody>();



}

void OnDrawGizmosSelected()
{
    Gizmos.color = Color.red;
    Gizmos.DrawWireSphere (transform.position, lookRadius);
}

}

1 个答案:

答案 0 :(得分:0)

您可以执行FindObjectOfType<Casting>().enabled = false

之类的操作

https://docs.unity3d.com/ScriptReference/Object.FindObjectOfType.html