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

How can i access another script function if the script to access is not attached to the same gameobject?

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 public 静态 ?

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

TeleportationsCore core = FindObjectOfType<TeleportationsCore>();

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

中找到更多信息

改变这个:

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

至:

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

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