• 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 /
  • Help Room /
avatar image
0
Question by Giovarco · Oct 20, 2017 at 12:15 PM · gameobject.findasteroids

GameObject.Find() returns null even if the gameObject I'm looking for is active

Hello,

I'm doing an asteroid-like game. I found an unfortunate problem. I would like to write this on one of my scripts:

 void Awake()
     {
         // Get game settings
         gameSettings = GetComponent<GameSettings>();
     }
 
 void Start() {
         // Get the asteroid containers
         asteroidContainer = GameObject.Find("Asteroids"); // GameObject.Find() returns null here
 }

But only this is working:

 void Awake()
     {
         // Get game settings
         gameSettings = GetComponent<GameSettings>();
 
         // Get the asteroid containers
         asteroidContainer = GameObject.Find("Asteroids"); // This works
 
     }

This does not road-block me, but I always thought that Awake() was used to setup the actual gameobject and Start() to setup "links" to other external (not the actual gameobject) resources.

I will attach an image of Unity, if it can help: Unity

immagine.png (140.0 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Giovarco · Oct 20, 2017 at 12:54 PM

Even if I'm not sure how to solve it, I found the problem!

GameObject.Find() works correctly. The fact is that one of public functions of my script Factory.cs is used before Factory Start() is called. I will explain it: I have another script called LevelMaker.cs which generates the level for me and this generation is made inside LevelMaker Start(), but It seems that while I generate the level in LevelMaker Start(), Factory Start() is still not called. This is why it works if I move the code to Factory Awake(). The fact now is: do you think that this behaviour is nice? For me, this should be addressed somehow because it could be confusing.

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

117 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

Related Questions

Make a game object face another game object when instantiated 2 Answers

Make enemy's weapon chase a game object 1 Answer

Help with arrays and checking other object's floats 0 Answers

GetComponent and GameObject.Find stopped working 0 Answers

using gameobject.find('key') but i want to make it with a tag 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges