Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by yigitcan · Dec 28, 2011 at 03:39 AM · instantiateprefabnullnamegame object

Game Object seems empty after instantiation

Hello, I am creating a new Game Object from a prefab, with Instantiate() method. However, whenever try to access a property of the object I created, it seems to be null. So for example what is the best way to create a instantiate a gameobject and change its name to something else right after. My code is here:

 for (int y = 0; y < 5; y++) {
                 for (int x = 0; x < 5; x++) {
           
                 spawnPos = new Vector3(brickPiece.renderer.bounds.size.x*x,brickPiece.renderer.bounds.size.y*y,0);
                 GameObject newObj = new GameObject();
                     
                 newObj    = (Instantiate( brickPiece, spawnPos, Quaternion.identity)) as GameObject;
                 newObj.name  = "brick "+(5*y+x); // can not change cuz newObj seems null
                     
                 }
         }

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 Julien-Lynge · Dec 28, 2011 at 04:29 AM 0
Share

For one thing, you're creating two gameObjects with this code. The line

GameObject newObj = new GameObject();

instantiates an empty game object in the scene.

Try

GameObject newObj = (Instantiate( brickPiece, spawnPos, Quaternion.identity)) as GameObject;

ins$$anonymous$$d.

Now on to your main problem: can you paste the specific error message that you're getting when you try to set newObj.name?

avatar image yigitcan · Dec 28, 2011 at 08:50 AM 0
Share

yes you are right. I removed that line and made it exatcly as you said. Now, whenever I try to do newObj.name, or anything else, it says : "NullReferenceException: Object reference not set to an instance of an object."

I somehow got over the problem by defining a 'var' ins$$anonymous$$d of a 'GameObject' on that line. However, a 'var' doesnt have the same properties with GameObject that I can work with.

1 Reply

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

Answer by Julien-Lynge · Dec 28, 2011 at 04:26 PM

The 'var' workaround is definitely a clue - we know your expression is returning something, the only question is what. You can use the following to print out the type:

Debug.Log(newObj.GetType());

Just put that immediately above the newObj.name assignment. I copied your code and ran it, and it worked without error on my end. The only thing I can think of is that there is something amiss with brickPiece. Can you try replacing brickPiece in the Instantiate with 'new GameObject()' and see if that works?

Comment
Add comment · Show 3 · 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 Julien-Lynge · Dec 28, 2011 at 04:35 PM 1
Share

Also, just as a test, make sure you use 'Object.Instantiate' explicitly so you're not accidentally calling some other Instantiate function. According to the 'as' keyword definition (http://msdn.microsoft.com/en-us/library/cscsdfbt%28v=vs.71%29.aspx), 'as' will return null if the type can't be converted. If you want to more explicitly force the conversion, you can use

GameObject newObj = (GameObject)Object.Instantiate( brickPiece, spawnPos, Quaternion.identity);

That will throw an error rather than silently returning null if the type can't be converted.

avatar image yigitcan · Dec 28, 2011 at 05:26 PM 0
Share

I checked the type and it returned a transform object. So i casted the var object to a Transform and was able to access and use all the properties I need.

When I tried

GameObject newObj = (GameObject)Object.Instantiate( brickPiece, spawnPos, Quaternion.identity);

it threw a InvalidCastException and Unity crashed inexplicably. I dont even try to cast newObj to anything else. Weird.. But my problem is solved by casting 'var' to 'Transform' actually. Not sure if its a good thing to do:)But Thank you!

avatar image Julien-Lynge · Dec 28, 2011 at 05:36 PM 1
Share

Not sure why you're having issues casting from Transform to GameObject. You can always access the gameObject through the transform. So if you have

Transform newTrans = Object.Instantiate( brickPiece, spawnPos, Quaternion.identity);

you can use

GameObject newObj = newTrans.gameObject;

or get it directly from the instantiate:

GameObject newObj = (Object.Instantiate( brickPiece, spawnPos, Quaternion.identity)).gameObject;

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.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

The prefab you want to instantiate is null. 4 Answers

Does changing the name property of a Prefab Clone have side effects? 1 Answer

Naming Instantiated Prefabs 2 Answers

Calling prefabs by name or tag returns null. 1 Answer

Issues with tracking prefab instances and gameobject naming (JS) 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