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

Show more comments

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.

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

Move bullet in the same direction of mouse 1 Answer

Object Pooling issue. GameObject instantiating 1 bullet instead of multiple bullets 1 Answer

Button not instantiating, no errors 2 Answers

Trying to implement a cone shot in a 2D Game. 1 Answer

instantiate objects within an area 2 Answers


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