I have a code that is working and I figured it was because the gameobject I wanted to find was from another scene, but I moved the gameobject to the scene that has the script looking for it… but it just doesn’t find it and I am almost completely sure that’s the problem, since I checked the name for typos, I checked with other objects and they were found. (Also the gameobject im trying to find is a canvas but i never had this problem before finding canvases)
SCRIPT IN THE CANVAS:
IEnumerator loadScene(string type)
{
DontDestroyOnLoad(gameObject);
UnityEngine.SceneManagement.Scene currentScene = SceneManager.GetActiveScene();
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(0, LoadSceneMode.Additive);
SceneManager.MoveGameObjectToScene(canvas.gameObject, SceneManager.GetSceneByName("MainGame"));
while (!asyncLoad.isDone)
{
yield return null;
}
SceneManager.UnloadSceneAsync(currentScene);
if (type == "host")
{
NetworkManager.Singleton.StartHost();
}
if (type == "join")
{
NetworkManager.Singleton.StartClient();
}
}
SCRIPT THAT SEARCHES FOR IT:
(runs after the object is moved)
public override void OnNetworkSpawn()
{
if (!IsOwner)
{
Destroy(this);
}
else
{
player = this.gameObject.GetComponent<Rigidbody>();
StartCoroutine(getName());
}
}
IEnumerator getName()
{
yield return new WaitForSeconds(0.1f);
TMP_InputField nameField = GameObject.Find("MenuGUI").transform.Find("NameField").GetComponent<TMP_InputField>();
name = nameField.text;
this.gameObject.name = name;
Destroy(nameField.transform.parent.gameObject);
}