• 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 Larry-Dietz · Nov 16, 2012 at 04:58 AM · instantiate prefab

Problem instantiating a prefab, and storing a reference to it.

I have the following code...

 GameObject go = Instantiate(Balloons[x],transform.position,transform.rotation) as GameObject;
 Debug.Log (go);

The object get instantiated just fine, but the Debug.Log shows go is NULL.

The Balloon array does contain prefabs. What is confusing me is that the object IS getting instantiated. I see the balloon on the screen. The code is instantiating the balloon, then trying to assign some values. When I got a null reference during the assignment, I added that Debug.Log line, to make sure that "go" was assigned the object, to decide if the null reference was accessing the component on the object, or if it was the object itself.

Below is the entire routine, in case I am missing something elsewhere.

 for (int x=0; x<=BalloonsToSpawn.Count-1; x++){
                 if( BalloonsToSpawn[x]>0){
                     int a = Random.Range (1,2500);
                     if (a<SpawnChance[x]){
                         BalloonsToSpawn[x]-=1;                        
                         GameObject go = Instantiate(Balloons[x],transform.position,transform.rotation) as GameObject;
                         Debug.Log (go);
                         go.GetComponent<Balloon>().BalloonID = BalloonID;
                         BalloonID+=1;                        
                     }
                 }
             }

Any idea what I am doing wrong here?

Any help is appreciated. -Larry

Comment
Add comment · Show 11
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 Seth-Bergman · Nov 16, 2012 at 05:06 AM 0
Share

just tested this, works fine for me.. are you sure there's not more you're not telling us?

avatar image Althaen · Nov 16, 2012 at 05:26 AM 0
Share

What exactly are you expecting the debug log to tell you?

avatar image sparkzbarca · Nov 16, 2012 at 05:41 AM 0
Share

maybe go.transform.name?

the as gameobject is pointless by the way. its implicit.

avatar image Seth-Bergman · Nov 16, 2012 at 05:45 AM 0
Share

@sparkzbarca - Not so, have you tried this? with UnityScript (JavaScript based) yes, but not with C#!

also, fyi the above debug will return the name of the game object as written (and at any rate you can access the name directly via gameObject... i.e.: "go.name"... so go.transform.name is a bit redundant

avatar image Perdog · Nov 16, 2012 at 05:48 AM 0
Share

Perhaps that particular index of the Balloons array he is calling is null?

avatar image sparkzbarca · Nov 16, 2012 at 05:50 AM 0
Share

you mean you have to use that for unityscript or for C#.

I use C# and i just instantiate, admittedly i've not yet bothered trying to store the object instantiated in a variable. But i've never used as

on a side note

you got like 4.5k karma answer this for me

http://answers.unity3d.com/questions/349071/splitting-a-script-up-into-multiple-scripts.html

avatar image Seth-Bergman · Nov 16, 2012 at 05:56 AM 0
Share

...gladly, though karma does not mean I actually know what I'm doing ;)

But I think I got this one.. :D

Oh, for C# you have to do lots of things EXPLICITLY, where UnityScript handles them implicitly... such as typecasting instantiated objects from type "Object" to type "GameObject".. but only IF you attempt to store a reference to the GameObject, that is ;)

avatar image PaulUsul · Nov 16, 2012 at 03:46 PM 1
Share

What type is the Balloons array? It should be GameObject. Even though "as GameObject" is the best try to cast ins$$anonymous$$d.

avatar image fafase · Nov 16, 2012 at 04:37 PM 0
Share

Instantiate returns an Object but the variable is GameObject. you do need to cast it.

There are two ways to do it:

 GameObject go = Instantiate(Balloons[x],transform.position,transform.rotation) as GameObject;

or

 GameObject go = (GameObject)Instantiate(Balloons[x],transform.position,transform.rotation);

They both returns null if not working but the second one throws an exception which gives information on where and why. The first one is faster.

When it comes to UnityScript it is all done for you by the compiler which makes it easier but also less safe.

avatar image aldonaletto · Nov 16, 2012 at 04:52 PM 0
Share

I suspect @PaulUsul is right: afaik, if Ballons isn't GameObject[], the typecast returns null without any warning. The object is instantiated, but the go variable will cause null reference errors.

avatar image Larry-Dietz · Nov 16, 2012 at 05:16 PM 0
Share

@PaulUsul is correct! Thank you Paul.

Paul, if you want to post this as an answer ins$$anonymous$$d of a comment, I will mark it as answered.

It was of type Transform. Correcting this to type GameObject fixed the issue!!

Thanks to everyone for your help!!!

-Larry

1 Reply

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

Answer by PaulUsul · Nov 16, 2012 at 06:02 PM

What type is the Balloons array? It should be GameObject. Even though "as GameObject" is the best way, try to cast instead it will throw an error if there is a problem.

Glad I could help :D

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 Larry-Dietz · Nov 16, 2012 at 07:41 PM 0
Share

Thanks again Paul!

-Larry

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

16 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

Related Questions

unity gameObject set id before instantiation 3 Answers

Having problem with Instatiate GameObject at Canvas 0 Answers

Im trying to instantiate prefab from an array but get a confusing response...(this prefab will be consumed by the player meaning it needs to be instantiated multiple times) 2 Answers

Prefab attached to script is destroying itself before Instantiate() is called 1 Answer

Spawn a prefab off screen? 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