- Home /
Question by
Spardom · Apr 25, 2018 at 11:06 AM ·
gameobjectscript.programming
Hi im trying to set a Gameobject variable in a script not in the inspector.
public GameObject planet;
void Start () {
planet = GameObject.Find("Planet");
}
I tried a few different things but in the inspector the game object is still blank when i try to test it. I would prefer not to use prefabs if possible.
Comment
Best Answer
Answer by vinilly · Apr 25, 2018 at 05:31 AM
[SerializeField]
private GameObject[] Planets;
void Update()
{
Planets = GameObject.FindGameObjectsWithTag("Planet");
}
I hope this helps :D
this way works, for some reason when I put it in the start function it doesn't do anything but when its in update it works even with my original code
Answer by MT369MT · Apr 25, 2018 at 05:05 AM
Are you sure that the GameObject called “Planet” exist in your current scene? If it is a prefab that is not in the scene you can’t decleare it so.
Answer by KittenSnipes · Apr 25, 2018 at 08:50 AM
@Spardom You could try this. Hope it works cheers.
public GameObject planet;
void Start() {
if (planet == null) {
planet = GameObject.FindGameObjectWithTag(“planet”);
}
}