Quantcast
Channel: Questions in topic: "dontdestroyonload"
Viewing all articles
Browse latest Browse all 416

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

$
0
0
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(); DontDestroyOnLoad(transform.gameObject); } // Update is called once per frame void FixedUpdate() { if (gameObject.GetComponent().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().enabled = false; Cursor.lockState = CursorLockMode.None; creatureEncounteredGO = other.gameObject.GetComponent().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().GetEncounteredCreature(); GameObject enemyCreatureGO = Instantiate(encounteredCreature, enemySpot);

Viewing all articles
Browse latest Browse all 416

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>