GameObjects destroyed on camera transitions when maximized on play

Hello!

Imgur Link to issue : Weird Unity Behaviour? - Album on Imgur

I’ve created scene transitions where the camera pans to a certain clamp and follows the players within those clamps. My character is an archer, which has a child object that is a bow sprite that works with screen to mouse position.

When I go from the first transition to the next scene, the bow is visible and I can shoot (on maximized on play as well as not maximized). When I do another scene transition (to any other transition), the bow gets destroyed and prompts me an error that the Transform for that bow has been destroyed when I’m maximized on play. But does not throw me the same error and works as intended when non-maximized on play.

I’m really not sure what I’m doing wrong here. Attached above is (hopefully) a clear video of what’s going on.

Please note I’m new at coding (I know it’s messy). Is there a different way to do this for “Maximizing on Play”?

Here’s my code that is “bugging” the system, the line : arrowHand.localScale = Vector3.one;
:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour

   
{
    public static PlayerController instance;
    public Rigidbody2D theRB;
    public float moveSpeed;
    public Animator anim;
    public Transform arrowHand;
    public GameObject interactable;
    public SpriteRenderer interactableSR;
    public int currentArrowCount;
    public int maxArrowCount;

    public SpriteRenderer theSR;

    public bool canMove;

    private Vector2 moveInput;

    public string areaTransitionName;

    public bool hasOrchardKey;
    public bool hasMetMarie;

    //public float minX, maxX, minY, maxY; //Clamp controllers

    // Start is called before the first frame update
    void Start()
    {
        if (instance == null)
        {
            instance = this;
        }
        else
        {
            Destroy(gameObject);
        }
        DontDestroyOnLoad(gameObject);
        DontDestroyOnLoad(arrowHand);
        interactable.SetActive(false);
        canMove = true;
    }

    // Update is called once per frame
    void Update()
    {   //movement
        if (canMove)
        {
            moveInput.x = Input.GetAxisRaw("Horizontal");
            moveInput.y = Input.GetAxisRaw("Vertical");
            moveInput.Normalize();

            theRB.velocity = moveInput * moveSpeed;

            if (moveInput != Vector2.zero)
            {
                anim.SetBool("isMoving", true);
            }
            else
            {
                anim.SetBool("isMoving", false);
            }
        }

        else
        {
            theRB.velocity = Vector3.zero;
        }

        //color change when damaging player
        if (PlayerHealthController.instance.isHurt)
        {
            theSR.color = new Color(1f, 0f, 0f, theSR.color.a);
        }


        else
        {
            theSR.color = new Color(1f, 1f, 1f, theSR.color.a);
        }

        //Setting the cursor and aim and flipping the player if mouse vector changes
        Vector3 mousPos = Input.mousePosition;
        Vector3 screenPoint = Camera.main.WorldToScreenPoint(transform.localPosition);


        if (mousPos.x < screenPoint.x)
        {

            transform.localScale = new Vector3(-1f, 1f, 1f);
            arrowHand.localScale = new Vector3(-1f, -1f, 1f);
            interactableSR.flipX = true;
        }
        else if (mousPos.x > screenPoint.x)
        {

            transform.localScale = Vector3.one;
            arrowHand.localScale = Vector3.one;
            interactableSR.flipX = false;
        }

    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if(other.tag == "Enemy")
        {
            Vector2 difference = (transform.position - other.transform.position);
            transform.position = new Vector2(transform.position.x + difference.x, transform.position.y + difference.y);
        }


    }

    private void OnTriggerStay2D(Collider2D other)
    {
        if (other.tag == "Marie" && Input.GetKeyDown(KeyCode.Space))
        {
            hasMetMarie = true;
        }

    }


}

Alright I had a good night’s sleep and a think about the issue that I posted last night, and woke up this morning to both maximize on play and non destroying my child game object (Reference to issue : Weird Unity Behaviour? - Album on Imgur)

My understanding when it comes to “DontDestroyOnLoad(gameObject)” was that it preserves even the Child objects. But it seems that it’s destroying only the Arrow Hand child object. This isn’t intended I really hope?

Also following up, the arrow Hand works with a worldToScreenPoint. And since the camera pans I’m not sure if it affects this function adversely. Strangely again, if I move from the first transition to the second, it preserves the Arrow Hand, but from second to any other transition, it destroys the hand as seen in the Imgur

Issue resolved. There was an error in logic in another script in the child object

1 Like