- Home /
GameObject.Find() not working when connected to prefab
I'm creating a game involving powerups. To spawn these powerups, I have a prefab of a powerup that is instantiated into the game every few seconds. The powerup I am currently testing is a powerup that spawns the walls around the edge of the screen when collided with. These walls prevent you from knocking your opponent off the platform and winning. However, it appears that only one instance of my prefab is usable at any one time, so colliding with a powerup will frequently not do anything (except give an error message) and leave the powerup there. The error message reads:
NullReferenceException: Object reference not set to an instance of an object
WallPowerup.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/WallPowerup.cs:21)
The line numbers pointed to give me reason to suspect this block of code:
void OnTriggerEnter (Collider other)
{
if (other.gameObject.CompareTag ("Player") || other.gameObject.CompareTag ("Comp")) {
walls.SetActive (true);
gameController.StartCoroutine (gameController.DestroyWalls ());
Destroy (this.gameObject);
}
}
The walls variable is a GameObject set in the Start() function, with GameObject.Find(), and the line walls.SetActive(true) is the specific line the error message points to. Since the rest of OnTriggerEnter() isn't functioning either, I think whatever error is going on manages to stop this specific instance of the script, but not any of the scripts in other instances of the same prefab.
Answer by unidad2pete · Aug 06, 2017 at 07:52 PM
GameObjects set Inactive when you use GameObject.Find() are not found.
try start scene with object activated, then add new line to your start function.
void Start()
{
walls = GameObject.Find("GAME OBJECT NAME");
walls.SetActive(false);
}
Other way to find gameObjects inactived on scene is searching a child Transform inactived on a active gameObject, gameObjects not active in scene are not found, but transform childs not active do it.
public GameObject wallParent; // EMPTY ACTIVE GAME OBJECT
public GameObject wall; // YOUR OBJECT, CHILD OF wallParent AND NOT ACTIVE
void Start ()
{
wallParent = GameObject.Find("WALL PARENT NAME");
wall = wallParent.transform.Find("YOUR GAME OBJECT NAME").gameObject;
}
Are you saying that I can't use GameObject.Find() on inactive objects? In that case, I could access the walls variable from a script that has a value for the walls variable before they are deactivated?
Yes, you cant found inactive objects with GameObject.Find(), but you can get acces all scripts when you have the object reference.
I mean, if you find an inactive object, you recive null and next code lines not continues. But if you found the object , dont care if the object is active or not, you have the reference to the object, and you can acces all scripts.
$$anonymous$$y updated script works now. Thanks for the help!
Answer by faithmorante · May 15 at 09:12 AM
I'm super late to the party but transform.Find works on inactive objects. So what you can do is nest the inactive objects in an empty Game Object, then find that parent GameObject then find the inactive object
GameObject.Find("Main").transform.Find(gameObject).gameObject.SetActive(true);
Your answer
![](https://koobas.hobune.stream/wayback/20220612132104im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Counting prefabs in screen?,Problem with counting prefabs in screen 1 Answer
how do I assign a prefab's child to another prefab's script's public variable? 1 Answer
Running scripts from editor 1 Answer
Problems calling functions from Scriptable Objects 0 Answers
Problem with multiple instances of script displaying same text object 2 Answers