Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Apr 25, 2019 at 11:39 PM by FelineFriendly for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by FelineFriendly · Apr 25, 2019 at 12:13 AM · 2dgameobjectnullreferenceexceptiongameobject.findquiz

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bruno2907 · Apr 25, 2019 at 12:56 AM 0
Share

$$anonymous$$y best guess is that you may have multiple GameObjects with this exact name and Unity is returning the wrong one

avatar image Hellium · Apr 25, 2019 at 06:10 AM 1
Share

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.

4 Replies

  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FelineFriendly · Apr 25, 2019 at 04:57 PM 0
Share

I will try to set it up to load that scene like you've suggested when I get a chance. Thanks for the recommendations!

avatar image
2

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FelineFriendly · Apr 25, 2019 at 02:19 PM 0
Share

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).

avatar image xxmariofer FelineFriendly · Apr 25, 2019 at 02:23 PM 0
Share

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

avatar image FelineFriendly xxmariofer · Apr 25, 2019 at 02:53 PM 0
Share

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."

Show more comments
avatar image
1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FelineFriendly · Apr 25, 2019 at 05:44 PM 0
Share

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!

avatar image FelineFriendly · Apr 25, 2019 at 07:06 PM 0
Share

@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!!!

avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

245 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D Platformer - Problem with object that kills, then restarts after build 1 Answer

Allocating Game Object name dynamically using Find 0 Answers

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

How to use the scene edit for Line Renderer? 1 Answer

2D Animation does not start 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges