【问题标题】:How to disable a script in another without Public GameObject? [duplicate]如何在没有公共游戏对象的情况下禁用另一个脚本? [复制]
【发布时间】:2018-04-19 16:37:14
【问题描述】:

我只想禁用我的玩家管理器中的脚本。但是因为我的敌人是通过Set Active 生成的,所以公共游戏对象无法正常工作。我试过让我的玩家经理也成为一个预制件,但没有解决方案。我可以通过代码行通过脚本中的特定游戏对象手动指定它吗?

尽管这看起来很小,但我确实做了研究,并且大多数都指向专门使用 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);
}

}

【问题讨论】:

    标签: unity3d


    【解决方案1】:

    您可以执行FindObjectOfType&lt;Casting&gt;().enabled = false 之类的操作。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多