• 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 post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by shavais · Oct 15, 2012 at 07:35 AM · javascriptinstantiateprefabsfind

finding instantiated prefabs

I have an object that has been instantiated like this:

 Instantiate(Resources.Load("MyPrefab"));

Visually I can see that that is happening when I test the program. In another script, after this has been done, I'm trying to reference that instance, but this code returns null:

 boss = GameObject.Find("MyPrefab");

Why? Shouldn't that return the first one currently in the scene? I guess GameObject.Find() doesn't find Instantiated objects? I looked for a "name" field in the prefab in case that was the source of the issue, but I'm not seeing one.

Comment
Add comment · Show 4
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 Eric5h5 · Oct 15, 2012 at 07:48 AM 0
Share

Look at the hierarchy when you instantiate an object...what do you see?

avatar image shavais · Oct 15, 2012 at 05:16 PM 0
Share

@fattie - Wow, interesting, thanks..

@eric - "$$anonymous$$yPrefab(Clone)" ? Really? Is that documented/reliable? Hmm, this code still says it's null, even though I see those showing up in the scene hierarchy tree:

 var prefab : GameObject = GameObject.Find("$$anonymous$$yPrefab(Clone)") as GameObject;
 Debug.Log("prefab is null: " + ((prefab == null) ? "true" : "false"));
avatar image shavais · Oct 15, 2012 at 08:16 PM 0
Share

I put in some test code that instantiates it and then looks for it, and it does find it, using that name. So, it's some kind of other problem with my code. But this is good to know, thanks again for the response.

avatar image Eric5h5 · Oct 15, 2012 at 09:41 PM 0
Share

If you don't want (Clone) added, you can rename it when you instantiate it.

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by ThePunisher · Oct 15, 2012 at 05:53 PM

Could it be that it's not the real name of the instance? Just trying to figure out if that's a possibility.

I would try renaming the GameObject you just instantiated to something else, then looking for that name.

Oh, another thing....are you sure your code which is looking for the GameObject is happening after the object has been Instantiated? I would use a debug statement to very the order.

If you are instantiating and looking in different Start/Awake methods it could be that the wrong Start/Awake is happening first.

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 shavais · Oct 15, 2012 at 08:26 PM 0
Share

I put in some test code that instantiates it, renames it, then looks for it under that name, and sure enough, it finds it. So it's like you're saying, there's some kind of an ordering issue in my code. I don't see how that's possible, because the code that is looking for it is kicked off by the code that first instantiates the prefab, but somehow it must be. Anyway, that's interesting, you can rename an object after instantiating it, then look for it by that name. That's good to know, thanks for the response.

avatar image ThePunisher · Oct 15, 2012 at 08:44 PM 0
Share

No problem man, glad it fixed it.

avatar image
6

Answer by Brenden-Frank · Nov 11, 2012 at 06:50 AM

This may be a little late and I know this was already answered, but you can very easily refer to your instantiated prefab by creating a variable and making it equal to the instantiate's return value:

 GameObject myGameObject = Instantiate(Resources.Load("MyPrefab", typof(GameObject))) as GameObject;
 
 myGameObject.name = "SomePrefabName";

You can then send it to your other script via a public variable or store it here with something vague...like

 GameObject.Find("SomeOtherObject").GetComponent<SomeOtherScriptOnThatObject>().myOtherScriptGameObjectVar = myGameObject;

Bearing in mind of course that Find() and GetComponent() should be used as much as possible outside of frame loops.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

12 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

Related Questions

Found if instantiate position already occupied 0 Answers

Checking if object intersects? 1 Answer

How to check what prefabs exist in one of the project folders? (not necessarily in Resources) 1 Answer

Shotgun Spread Issue 1 Answer

Wrong coordinates of setting position of the object 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