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 Jwilson92 · Aug 27, 2013 at 02:26 PM · cameracamera-looknull reference

null reference exception when setting cam target?

hey,

'm fairly new to scripting in C# and have recently started to create a 2D platfromer after watching a few tutorials. I have have the controls, physics and camera following my player, but when attempting to implement a respawn I have a problem resetting the camera target. When the player object enters the death zone the script calls a die function that destroys the player then instantiates a new one.

whenever I try to set the camera target I get the error null reference exception any and can't seem to get around it. Any Ideas the code where the error occurs is below.

any Ideas the code where the error occurs is below.

 //actions to be excuted when die is called 
 public void Die(){
     
     
     Debug.Log("Destroy Player");
     
 //create new player 
     player = (GameObject) Instantiate(player,Vector3.zero,Quaternion.identity);
     
 //new player as camera target 
 cam.SetTarget(player.transform);
     
 //destroy current player
     Destroy(gameObject);//kill current player
     Debug.Log("lose life");
     
     
     
     
 }   ##
Comment
Add comment · Show 6
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 sdgd · Aug 27, 2013 at 02:27 PM 0
Share

we need your error log too

avatar image robertbu · Aug 27, 2013 at 03:37 PM 0
Share

You've only included part of your code, and a paste of the error from the console window is often helpful for these kinds of errors. I'm going to guess that the error is on line 11. Where does 'cam' get declared and initialized? SetTarget() is not a Camera function, so this has to be some script you've created or borrowed.

avatar image Jwilson92 · Aug 27, 2013 at 04:39 PM 0
Share

Yeah the error does occur on line 11 or wherever I place that line of code.

the error message I get is:

NullReferenceException: Object reference not set to an instance of an object PlayerControls.Die () (at Assets/Scripts/PlayerControls.cs:142) DeathScript.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/DeathScript.cs:24)

avatar image robertbu · Aug 27, 2013 at 04:40 PM 0
Share

What that tells you is that 'cam' has not been initialized. You've not included enough code for use to advise you on what to do to fix the problem. We need to see where the variable is declared and any code that tries to initialize the variable.

avatar image Jwilson92 · Aug 27, 2013 at 06:08 PM 0
Share

In the player controls scrip the cam varible is only used in start and the die function

 public GameObject player;
 

//////////////////////////////////////////////////////////////

 private gameCamera cam;


////////////////////////////////////////////////////////

 // Use this for initialization
 void Start () {
     
     cam = GetComponent<gameCamera>();

 }
 

/////////////////////////////////////////////////////////////////////////////////////// }

 //actions to be excuted when die is called 
 public void Die(){
     
 
     Debug.Log("Destroy Player");
     
 //create new player 
     player = (GameObject) Instantiate(player,Vector3.zero,Quaternion.identity);
         
 //new player as camera target 
         
 //destroy current player
     Destroy(gameObject);//kill current player
     Debug.Log("lose life");
     cam.SetTarget(player.transform);
     
     
     
 }

}

The variable is also used in a game manager script.

public class game$$anonymous$$anager : $$anonymous$$onoBehaviour {

 public GameObject player;
 private gameCamera cam;//create camera object
 

 // Use this for initialization
 void Start () {
     
     // get reference to to the game camera.
     cam = GetComponent<gameCamera>();//get acces to gamecamera script 
     
     //spawn player 
     SpawnPlayer();//execute spawn function 
             
 }
 
 /////////////////////////////////////////////////////////////////////////////
 
 // Spawn player
 public void SpawnPlayer() {
     //set camera target as player 
     cam.SetTarget((Instantiate(player,Vector3.zero,Quaternion.identity) as GameObject).transform);
                                         
     
 }
 

}

avatar image KiraSensei Jwilson92 · Aug 27, 2013 at 06:17 PM 0
Share

Don't post an answer, edit your question.

0 Replies

· Add your reply
  • Sort: 

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

17 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

Related Questions

Line problem 0 Answers

Player camera targeting/locking another object 0 Answers

Enable the OrbitCam script how can I make it will continue from the current camera view and position ? 0 Answers

Camera gets stuck when cursor is locked 0 Answers

How to make the second camera rotate automatically? 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