如果要访问的脚本未附加到同一个游戏对象,我该如何访问另一个脚本函数?

时间:2017-05-17 09:39:17

标签: c# unity3d unity5

using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;

 public class Teleport : MonoBehaviour {

     public GameObject player;
     public Camera mainCamera;
     public Camera firstCam;
     public Camera camera;
     private List<GameObject> TeleportBooths;

     private TeleportationsCore tc;

     private void Start()
     {
         InstantiateObjects gos = GetComponent<InstantiateObjects>();

         TeleportBooths = new List<GameObject>();
         TeleportBooths = gos.PrefabsList();
         firstCam.enabled = false;
         mainCamera.enabled = false;
         camera.enabled = true;

         for (int i = 0; i < TeleportBooths.Count; i++)
         {
             TeleportBooths[i].AddComponent<TeleportationsCore>();
         }
         tc = GetComponent<TeleportationsCore>();
         WorkingBooth();
     }

     private void WorkingBooth()
     {
         player.transform.position = TeleportBooths[tc.WorkingBooth()].transform.position;
         camera.transform.position = new Vector3(TeleportBooths[tc.WorkingBooth()].transform.position.x - 10, TeleportBooths[tc.WorkingBooth()].transform.position.y + 10, TeleportBooths[tc.WorkingBooth()].transform.position.z);
         camera.transform.LookAt(TeleportBooths[tc.WorkingBooth()].transform);
     }

     private void Update()
     {
         WorkingBooth();
     }
 }

我正在做:

tc = GetComponent<TeleportationsCore>();

但是tc为空。

我要访问的脚本:

using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;

 public class TeleportationsCore : MonoBehaviour
 {
     public float spinSpeed = 2.0f;

     private bool rotate = false;
     private bool exited = false;
     private int boothIndex = 0;

     private void Start()
     {
         WorkingBooth();
     }

     void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.tag == "Player")
         {
             Debug.Log("Player entered the hole");
             rotate = true;
         }
     }

     private void OnTriggerExit(Collider other)
     {
         if (other.gameObject.tag == "Player")
         {
             Debug.Log("Player exited the hole");
             rotate = false;
             exited = true;
         }
     }

     void Rotate()
     {
         if (rotate)
         {
             if (spinSpeed == 350)
             {
                 rotate = false;
                 exited = true;
                 boothIndex++;
                 WorkingBooth();
             }
             else
             {
                 transform.Rotate(Vector3.up, spinSpeed * Time.deltaTime);
                 spinSpeed += 1f;
             }

         }
         if (rotate == false && exited == true)
         {
             transform.Rotate(Vector3.up, spinSpeed * Time.deltaTime);
             if (spinSpeed > 0.0f)
                 spinSpeed -= 1f;
         }
     }

     public int WorkingBooth()
     {
         return boothIndex;
     }

     private void Update()
     {
         Rotate();
     }
 }

我想要的是将脚本附加到所有游戏对象以访问TeleportationsCore上的功能WorkingBooth。

我并不想将TeleportationsCore附加到附加的GameObject Teleport上。那么还有什么方法可以访问TeleportationsCore上的WorkingBooth?使WorkingBooth公共静态?

2 个答案:

答案 0 :(得分:1)

将TeleportationsCore脚本附加到空游戏对象。要获得它的参考,请使用:

TeleportationsCore core = FindObjectOfType<TeleportationsCore>();

例如在Start函数中使用它,因为它有点慢。 您可以在documentation.

中找到更多信息

答案 1 :(得分:1)

改变这个:

 for (int i = 0; i < TeleportBooths.Count; i++) {
     TeleportBooths[i].AddComponent<TeleportationsCore>();
 }

为:

TeleportationsCore[] tCores = TeleportBooths.Select(booth => booth.AddComponent<TeleportationsCore>());

现在只需从列表中选择您想要的核心。