- Home /
The question is answered, right answer was accepted
GameObject.Find returning NullReferenceException
Hi everyone! I'm trying to create a quiz game from the tutorial provided by Unity. I finished said tutorial and added many features on my own. Right now, I am trying to create an optional timed mode that becomes available when the player gets a perfect score on the game. So far, I have made a separate TimeLimit script that holds all the code that runs the timed mode. If that script is enabled, timed mode runs smoothly. However, I am trying to make that script (or the GameObject housing it) inactive until the player gets a perfect score. I am trying to accomplish this currently by using the script below, which returns a NullReferenceException:
public void TimedMode()
{
GameObject.Find("TimeLimit").GetComponent<TimeLimit>().enabled = false;
}
I have checked my spelling and tried to deactivate the overarching TimeLimit GameObject (as opposed to the script in it), but neither has changed anything. I also have checked to make sure my GameObject is not null, so that isn't the problem either. The actual GameObject is in a separate scene from the one this script is in, but everything I've seen says GameObject.Find should search all scenes. If anyone has any ideas on how I can fix this problem (or if you need more info), please let me know! Thanks!
P.S. I should probably mention that my ultimate goal will be to start the script deactivated and then reactivate it with this script. Right now I am just trying to do the opposite so I can make sure it works and I understand it.
$$anonymous$$y best guess is that you may have multiple GameObjects with this exact name and Unity is returning the wrong one
Is TimeLimit
object activated or not when GameObject.Find is called? The latter can't find inactive objects.
If possible, I suggest you declaring a public TimeLimit
and drag&drop the gameObject in the inspector.
Answer by programer717 · Apr 25, 2019 at 03:22 PM
Gamobject.find searches all active scenes. It may be easiest to just make it so it is all on one scene. Another thing you could do is start with the TimeLimit script active on another scene, only make the option to load the other scene once you have a perfect score.
I will try to set it up to load that scene like you've suggested when I get a chance. Thanks for the recommendations!
Answer by jojizaidi · Apr 25, 2019 at 11:44 AM
Easiest would be to make a public reference to the TimeLimit script and drag drop the script on it in the Inspector.
Much more efficient than GameObject.Find too.
I would love to be able to do that, but the TimeLimit script and GameObject have to be in a different scene than the script above, so (to my knowledge) I can't drag it into the inspector. Tonight I will experiment and see if I can change the scene it's in, but I doubt I can. @Hellium This answers your idea too (and yes the object is activated).
well, you wont be able to drag and drop and wont be able top find it either, you cant have references to objects in other scenes
Are you sure? In the Scripting API for GameObject.Find, it says "If the game is running with multiple scenes then Find will search in all of them."
Answer by surfuay · Apr 25, 2019 at 05:04 PM
to jojizaidi's point,
in your time limit script add this to the top
//public so anyone can access it, static so that it's always there, TimeLimit (or what ever the name of you script actually is), timing (you can put whatever you want for that really.
public static TimeLimit timing;
then add this below it before your Start Method
public void Awake()
{
if (timing== null)
{
timing = this;
}
else if (timing!= this)
{
Destroy(gameObject);
}
DontDestroyOnLoad(gameObject);
}
this will make the script follow you through every scene, now when you try to access it in another script it will look like this.
TimeLimit.time.whatevertheboolyou'reaccess = false;
and you can access anything else you want too.
if the TimeLimit script is attached to an object thats running around your game (like, not an empty game Object) you might have to work some other magic.
I really REALLY appreciate that you wrote this up for me!! I am wondering if it will work, though, since I have tried using DontDestroyOnLoad on my GameObject already, but the scene with the GameObject doesn't open (and put the script in DontDestoryOnLoad) until AFTER the scene where timed mode would be turned on. I don't think I'm explaining this very well, so here's a quick run-through: Game opens on menu screen. On menu screen you can play a normal game of the quiz. There won't be an option for timed mode until a perfect score is earned, and the script + GameObject for timed mode would be inactive. Once the player gets a perfect score, the option should appear on the menu to play in timed mode, which would activate my script/GameObject. I'm honestly not even sure if it's fixable without major changes, but I will just keep experimenting with everyone's suggestions until it works!
@surfuay I just realized something; would putting the DontDestroyOnLoad command in Awake (like you did) put the GameObject there right when the game begins as opposed to when its scene loads? I didn't think about it until now, but if that's the case, that would very likely solve my problem and you're a genius!!!
Answer by FelineFriendly · Apr 25, 2019 at 11:38 PM
Hey everyone! I was able to solve the problem by just making everything happen in a single scene so I could drag and drop into the inspector. Thanks for all the help!