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);
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.