- Home /
Setting up reference to an inactive GameObject through script
Is this not allowed? I have tried setting up a reference to an inactive GameObject through script using the Awake() method. Unfortunately, I always end up with null. I know that it would work if I set up the references using the UnityEditor properties by dragging but I would prefer setting them up using a script in cases of having the reference holder being non-existent yet (Instantiating during run time).
If the script is attached to the game object that is inactive then nothing would work in that script
There's always different ways of getting round things, what is it you want to get access to for that GameObject?
Chances are it can either be handled when the object gets instantiated or held in another GameObject and only used by that GameObject when it's active.
I have a "gameObject A" with "Script A" that does not exist yet because it is to be instantiated during runTime. I also have "gameObject B" that currently exists in the game already but is Inactive.
After the "gameObject A" is instantiated, I want it's "Script A" to set up a reference to the inactive "gameObject B".
The problem is that whenever I set the reference using "script A" gameObject reference = GameObject.Find("gameObject B"); It always ends up null.
Answer by Mmmpies · Jan 10, 2015 at 07:38 PM
OK, so instead of setting that when gameObjectA is instantiated why not do this...
Make gameObjectB a prefab and instantiate it when needed, from the script that instantiates it hold a reference to ScriptA and have a public function in ScriptA to pickup gameObjectB and when you instantiate gameObjectB call that function just afterwards.
It should pick up the newly instantiated object once it's there.
Do you need help with the scripting and if yes javascript or C#?
I'm very sorry for the late reply but I will try out your method.
Also, thank you but I don't need help with scripting. I like doing scripts myself since I see it as learning.
Your answer
Follow this Question
Related Questions
How Can i Reference String another Script? 2 Answers
How do I reference an object's rotation through an attached script? 1 Answer
"Object Reference not set to an instance of an object" 1 Answer
How to reference another script and call a function in C# ? 2 Answers
Does a reference to a script have up-to-date info ? 1 Answer