- Home /
GameObjet.find give "Missing"
Hello everyone, am facing a strange problem here, i have a class named "MainCowboy" and game objects that use this class are with tag "cowboy", these objects are a child of prefabs (lvl1, 2, 3, etc..)
now the problem is when i load a level (any) for the first time, Game.Object.FindWithTag works perfectly as expected, however, once i load another level, the variable with type MainCowboy become "Missing (MainCowboy)" thank you EDIT : here is the class that contain the error, i removed all the unnecessary lines just to make it more clearer, the loadLevel() method is called when i press a GUI button in the guiManager class .
using UnityEngine;
using System.Collections;
public class GameManger : MonoBehaviour
{
public MainCowboy cowBoy ;
public GameObject[] prefabs ;
public GameObject level;
void Start ()
{
loadLevel (1);
cowBoy = GameObject.FindWithTag ("cowboy").GetComponent<MainCowboy> ();
}
public void loadLevel (int id)
{
Destroy (level);
level = Instantiate (prefabs [id], transform.position, transform.rotation)as GameObject;
cowBoy = GameObject.FindWithTag ("cowboy").GetComponent<MainCowboy> ();
}
}
It would be easier to answer if you could give code from your class and the structure of your GameObjects.
Is there an actual error co$$anonymous$$g up on your log? If so what's the exact error.
@Cains8 no initially i don't get any error when that happens, however when i try to use "cowBoy" after that i get an error who say that the object doesn't exist
Answer by Starwalker · Nov 10, 2013 at 08:55 AM
Whatever object you have this script applied to and the target object this script is referring to is getting destroyed on scene load. It happens by default.
Suggest using this: Dont destroy on load reference
The above code would keep the object alive between scene changes. And apply the same restriction to both objects in question depending on what you require to be alive and wake in the next scene.
Hope it helps
thanx for your answer, but just for the reference, am not loading any new scene (unity scenes) the levels are prefabs and everything is happening in the same scene