• 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 /
  • Help Room /
avatar image
Question by KIMSCHMITZ · Nov 09, 2015 at 07:03 AM · c#instantiateobjectnull reference exception

Object reference not set message, directly after instantiation

Hi Guys,

i have a LevelController script which is loading some data and then instantiates the Objects. As you can see, right after i instantiate my player Prefab, i tell my Camera about it, so my Camera is able to follow my player.

Problem is: The CameraController Scripts tells me, that there is no reference.

Setting Camera, player Gameobject and Camera ControllerScript in my LevelController which builds the Level (and so instantiates my player)

     public Camera cami;
     private CameraController cc;
     private GameObject go_player;
     cc = cami.GetComponent<CameraController>();

Later i do the instantiation and tell my Camera who the player is:

 case  3:
 go_player = Instantiate(Player_PreFab, position, Quaternion.identity) as GameObject;
 cc.setTarget(go_player); // <----- I am telling my Camera about the player Gameobject


Then, in my CameraController i have the "setTarget" function i addressed above:

 public void setTarget(GameObject go)
 {
     Debug.Log ("this function has been called");
     target = go;
     Debug.Log(go.transform.position);
 }

Due to the Debug.Log i can see, that the function has been called, but right after that i receive this:

 > NullReferenceException: Object
 > reference not set to an instance of an
 > object CameraController.setTarget
 > (UnityEngine.GameObject go) (at
 > Assets/Scripts/CameraController.cs:24)
 > LevelController.GenerateTiles (Int32
 > height, Int32 width) (at
 > Assets/Scripts/LevelController.cs:56)

For me that sounds like: The instantiated GameObject player is not known by my cameraController. But, i gave the Gameobject player to the CameraController right after its instantiation.

Any help would be highly appreciated. KIM

Comment

People who like this

0 Show 1
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 KIMSCHMITZ · Nov 07, 2015 at 04:45 PM 0
Share

Ok, what i found till yet: CameraController Scripts are fine, but getting some data from my player is also not working right after instantiation.

 case  3:
 go_player = Instantiate(Player_Prefab, position, Quaternion.identity) as GameObject;
 Debug.Log (go_player.transform.position); //<-- throws exception 
 cc.setTarget(go_player);
 break;

As i can see the player object flying around, i wonder how this can happen. Maybe i am calling the player game objects too fast after instantiation?

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by KIMSCHMITZ · Nov 15, 2015 at 04:55 PM

And here is the answer to my problem.

I assigned Player_Pref via Drag& Drop in the inspector. Later i decided to change Player_Prefab to be a GameObejct (it was Transform before). I did so in my Script.

Of course(?) the assignment in the inspector got lost, but i did not look after this. Again a stupid thing - but this is how to learn :-) I reassigned the prefab and everything is working fine now.

I also learned that i do like more the programmatic assignment :-).

(And thanks to a colleague in my working place who showed me the way) Kind regards, KIMSCHMITZ

Comment

People who like this

0 Show 0 · 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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Problema al crear objeto por segunda vez. Problem to instantatiate a object for second try. 1 Answer

How do you pick up an instantiated object? 1 Answer

*Solved* Loop instantiating objects causes my Unity Editor to crash 1 Answer

Instantiate object (on touch position) by touch on the collider of other object 1 Answer

Getting error NullReferenceException on instantiate 3 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