- Home /
Instantiated Prefab doesn't find main camera
I'm calling a basic GameObject.Find("CameraName") in the start of my script for a prefab that gets instantiated in the scene. When I have the prefab in the scene when I run it, it works fine and finds the camera. but when I instantiate the prefab it can't find the camera, so its something to do with the instantiation. I've never seen this before! any ideas?
Answer by Collywog · Mar 16, 2019 at 02:26 AM
The start script runs when the game starts, not when the object prefab is created if I remember correctly.
Instead of putting it in start try putting it in its own function and calling that functions or using a FixedUpdate() method
Answer by meMUmar · Mar 16, 2019 at 06:35 AM
Yes because when your start function runs, prefab is not instantiated. So make sure when your prefab is instantiated then find your object. Here is an example how you can find the object:
GameObject spawnerPoint;
void FindSpawnerPoint() {
spawnerPoint = GameObject.FindGameObjectWithTag("HeliSpawner");
}
void Update()
{
if(!spawnerPoint) {
FindSpawnerPoint();
return;
}
}
I think this will solve your problem. Good day.
Your answer
Follow this Question
Related Questions
What is the best way to instatiate an array of connected GameObjects? 0 Answers
Store Game Object Into List For Later Reinstantiation? 0 Answers
Destroying childs and Instantiate [C#] 0 Answers
Instantiated GameObject gets spawned as a child 2 Answers
How to randomly spawn three non repeating gameobjects from an array? 2 Answers