Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by seanwiemken · Aug 09, 2021 at 12:15 AM · nullreferenceexceptiondontdestroyonload

Why is the value of my variable being reset when changing scenes?

I have an object that in the start function it has DontDestroyOnLoad(theGO). Just before I change scenes I get a reference to a prefab and set it to creatureEncounteredGO, then change the scene. I have used the visual studios debug tool and confirmed it does set the variable to the proper value. But once I load into the new scene the value becomes null. I don't understand why though. I would greatly appreciate some help with this. Thanks in advance. Here is the code:

 public class scrPlayer : MonoBehaviour
 {
     [SerializeField] Camera playerView;
     [SerializeField] Transform groundObject;
     [SerializeField] GameObject playerCam;
 
     public float mouseSensitivity = 50f;
     public float speed = 6f;
     public float gravity = -9.81f;
     public float groundDistance = .4f;
     public LayerMask groundMask;
     public float jumpHeight = 1.5f;
 
     private GameObject creatureEncounteredGO;
     private float mouseX;
     private Vector3 velocity;
     private CharacterController controller;
     private bool isOnGround;
     private Vector3 preComabtLocation;
 
 
     // Start is called before the first frame update
     void Start()
     {
         if(SceneManager.GetActiveScene().Equals("FirstTown"))
             Cursor.lockState = CursorLockMode.Locked;
         controller = GetComponent<CharacterController>();
         DontDestroyOnLoad(transform.gameObject);
     }
 
     // Update is called once per frame
     void FixedUpdate()
     {
         if (gameObject.GetComponent<CharacterController>().enabled)
         {
             isOnGround = Physics.CheckSphere(groundObject.position, groundDistance, groundMask);
 
             if (isOnGround && velocity.y < 0 && !Input.GetButtonDown("Jump"))
                 velocity.y = -2f;
 
             mouseX += Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
 
             float x = Input.GetAxis("Horizontal");
             float z = Input.GetAxis("Vertical");
 
             this.transform.rotation = Quaternion.Euler(0, mouseX, 0);
 
             Vector3 move = transform.right * x + transform.forward * z;
             controller.Move(move * speed * Time.deltaTime);
 
             if (Input.GetButton("Jump") && isOnGround == true)
                 velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
 
             velocity.y += gravity * Time.deltaTime;
 
             controller.Move(velocity * Time.deltaTime);
         }
     }
 
     private void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.CompareTag("OWCreature"))
         {
             preComabtLocation = transform.position;
             playerCam.SetActive(false);
             gameObject.GetComponent<CharacterController>().enabled = false;
             Cursor.lockState = CursorLockMode.None;
             creatureEncounteredGO = other.gameObject.GetComponent<scrOverWorldCreatures>().GetCreatureGO();
             Debug.Log($"Creature Encountered: {creatureEncounteredGO}");
             SceneManager.LoadScene("CombatScene");
         }
     }
 
     public GameObject GetEncounteredCreature()
     {
         return creatureEncounteredGO;
     }

and the code that tries to get this information

  GameObject encounteredCreature = playerGO.GetComponent<scrPlayer>().GetEncounteredCreature();
  GameObject enemyCreatureGO = Instantiate(encounteredCreature, enemySpot);



Comment
Add comment
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

1 Reply

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

Answer by seanwiemken · Aug 09, 2021 at 06:26 AM

I found the problem, I will list it here just in case someone else has this problem. The issue was in the second block of code playerGO was referencing the prefab, which does have a null value for creatureEncounteredGO. The fix was to reference the player object by using the GameObject.Find(objectName) in the start method in the script I was using to set the creature object in. Hope someone finds this information useful.

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

169 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

DontDestroyOnLoad works in editor but not on android target 4 Answers

DontDestroyOnLoad() not working? 1 Answer

I am having problems with saving and loading when I go back and forth between scenes. 1 Answer

Upgrading to Unity 5.2 - All Image (Script) components now failing with NullReferenceException in GetStencilDepth? 0 Answers

Why this if logic isnot short circuiting? 0 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