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 Atro · Jun 12, 2014 at 04:28 PM · gameobjectinstantiateinstance

C# Object reference not set to an instance of an object right after GameObject.Instantiate

Hi guys, im pretty new to c# and unity, so it may be a stupid question.

Basically i'm trying to Instantiate 3 (or a variable amount) of game object from a prefab (wich is bound in unity onto script on other game object) somewhere "on the edge of the screen". However, when i try to acces the newObject to change its name i get that error mentioned in the title.

     using System;
     using UnityEngine;
     
     namespace Assets.Code.States
     {
         public class Generator
         {
             private References refInst;
             private Vector3 goThere;
             private Transform objectPrefab;
             
             
             public Generator ()
             {
             }
             
             public void CreateObjects()
             {
                 refInst = GameObject.Find ("GameManager").GetComponent<References>();
                 objectPrefab = refInst.bigObjectPrefab1;
     
                 int amount = 3;
                 
                 for (int i = 0; i < amount; i++)
                 {
                     float whereDoIGo = UnityEngine.Random.value * 4;
                     
                     if (whereDoIGo <= 1)
                     {
                         goThere = Camera.main.ViewportToWorldPoint (new Vector3(UnityEngine.Random.value, 0.99f, 10));
                     }
                     
                     if (whereDoIGo > 1 && whereDoIGo <= 2)
                     {
                         goThere = Camera.main.ViewportToWorldPoint (new Vector3(UnityEngine.Random.value, 0.99f, 10));
                     }
                     if (whereDoIGo > 2 && whereDoIGo <= 3)
                     {
                         goThere = Camera.main.ViewportToWorldPoint (new Vector3(UnityEngine.Random.value, 0.99f, 10));
                     }
                     if (whereDoIGo > 3 && whereDoIGo <= 4)
                     {    
                         goThere = Camera.main.ViewportToWorldPoint (new Vector3(0.01f, UnityEngine.Random.value, 10));
                     }
                     
                     GameObject objectNew = GameObject.Instantiate(objectPrefab, goThere, Quaternion.identity) as GameObject;
 
                     objectNew.name = "Hi there! " + i;
                     
                 }
                 
             }
         }
     }
     
 
Comment
Add comment · Show 3
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 andrew-lukasik · Jun 12, 2014 at 10:49 PM 1
Share

Are you sure that objectPrefab is not null? To make sure you may want to add "Debug.Log("objectPrefab.name="+objectPrefab.name);" line before Instantiate

avatar image Atro · Jun 13, 2014 at 08:30 AM 0
Share

Yes, tried it now just to be sure, its not null. All 3 objects get instantiated fine if i leave out objectNew.name = ....; or some other acces to the newly created object like position etc.

avatar image Kiwasi · Jun 13, 2014 at 09:19 AM 0
Share

Which line is throwing the error?

Try Debug.Log (objectNew);

If this is fine run the same thing on goThere and objectPrefab

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Atro · Jun 13, 2014 at 11:10 AM

Ok, thx guys, got it fixed. BoredMoron ( nice nick dude ^^ ) pitched a nice idea, objectNew was indeed returning as null.

Thx to the answer here i figured out that "as GameObject" cast actually hides an error and returns null and by "hardcasting" GameObject objectNew = (GameObject) GameObject.Instantiate(objectPrefab, goThere, Quaternion.identity); i get "InvalidCastException: Cannot cast from source type to destination type" wich "as" cast was actually hiding.

So i fixed it by changing instantiate into Transform objectNew = (Transform)GameObject.Instantiate(objectPrefab, goThere, Quaternion.identity);

Im bit dazzled by this voodoo but everything seems to work as a charm now.

Thx for ur help.

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.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

Instantiated GameObject being deleted when destroying original 1 Answer

Instanciate gameobject from Resources subfolder 1 Answer

Issues with tracking prefab instances and gameobject naming (JS) 1 Answer

Continually Updating position of Instantiated Object (to Mouse Pointer) 0 Answers

Spawning GameObject contained within another 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