Search Unity

GameObjects destroyed on camera transitions when maximized on play

Discussion in 'Getting Started' started by yummynath, Jul 22, 2020.

  1. yummynath

    yummynath

    Joined:
    Jul 3, 2020
    Posts:
    19
    Hello!

    Imgur Link to issue : https://imgur.com/a/TnvEwQf

    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;
    :


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6.  
    7.    
    8. {
    9.     public static PlayerController instance;
    10.     public Rigidbody2D theRB;
    11.     public float moveSpeed;
    12.     public Animator anim;
    13.     public Transform arrowHand;
    14.     public GameObject interactable;
    15.     public SpriteRenderer interactableSR;
    16.     public int currentArrowCount;
    17.     public int maxArrowCount;
    18.  
    19.     public SpriteRenderer theSR;
    20.  
    21.     public bool canMove;
    22.  
    23.     private Vector2 moveInput;
    24.  
    25.     public string areaTransitionName;
    26.  
    27.     public bool hasOrchardKey;
    28.     public bool hasMetMarie;
    29.  
    30.     //public float minX, maxX, minY, maxY; //Clamp controllers
    31.  
    32.     // Start is called before the first frame update
    33.     void Start()
    34.     {
    35.         if (instance == null)
    36.         {
    37.             instance = this;
    38.         }
    39.         else
    40.         {
    41.             Destroy(gameObject);
    42.         }
    43.         DontDestroyOnLoad(gameObject);
    44.         DontDestroyOnLoad(arrowHand);
    45.         interactable.SetActive(false);
    46.         canMove = true;
    47.     }
    48.  
    49.     // Update is called once per frame
    50.     void Update()
    51.     {   //movement
    52.         if (canMove)
    53.         {
    54.             moveInput.x = Input.GetAxisRaw("Horizontal");
    55.             moveInput.y = Input.GetAxisRaw("Vertical");
    56.             moveInput.Normalize();
    57.  
    58.             theRB.velocity = moveInput * moveSpeed;
    59.  
    60.             if (moveInput != Vector2.zero)
    61.             {
    62.                 anim.SetBool("isMoving", true);
    63.             }
    64.             else
    65.             {
    66.                 anim.SetBool("isMoving", false);
    67.             }
    68.         }
    69.  
    70.         else
    71.         {
    72.             theRB.velocity = Vector3.zero;
    73.         }
    74.  
    75.         //color change when damaging player
    76.         if (PlayerHealthController.instance.isHurt)
    77.         {
    78.             theSR.color = new Color(1f, 0f, 0f, theSR.color.a);
    79.         }
    80.  
    81.  
    82.         else
    83.         {
    84.             theSR.color = new Color(1f, 1f, 1f, theSR.color.a);
    85.         }
    86.  
    87.         //Setting the cursor and aim and flipping the player if mouse vector changes
    88.         Vector3 mousPos = Input.mousePosition;
    89.         Vector3 screenPoint = Camera.main.WorldToScreenPoint(transform.localPosition);
    90.  
    91.  
    92.         if (mousPos.x < screenPoint.x)
    93.         {
    94.  
    95.             transform.localScale = new Vector3(-1f, 1f, 1f);
    96.             arrowHand.localScale = new Vector3(-1f, -1f, 1f);
    97.             interactableSR.flipX = true;
    98.         }
    99.         else if (mousPos.x > screenPoint.x)
    100.         {
    101.  
    102.             transform.localScale = Vector3.one;
    103.             arrowHand.localScale = Vector3.one;
    104.             interactableSR.flipX = false;
    105.         }
    106.  
    107.     }
    108.  
    109.     private void OnTriggerEnter2D(Collider2D other)
    110.     {
    111.         if(other.tag == "Enemy")
    112.         {
    113.             Vector2 difference = (transform.position - other.transform.position);
    114.             transform.position = new Vector2(transform.position.x + difference.x, transform.position.y + difference.y);
    115.         }
    116.  
    117.  
    118.     }
    119.  
    120.     private void OnTriggerStay2D(Collider2D other)
    121.     {
    122.         if (other.tag == "Marie" && Input.GetKeyDown(KeyCode.Space))
    123.         {
    124.             hasMetMarie = true;
    125.         }
    126.  
    127.     }
    128.  
    129.  
    130. }
    131.  
     
  2. yummynath

    yummynath

    Joined:
    Jul 3, 2020
    Posts:
    19
    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 : https://imgur.com/a/TnvEwQf)

    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
     
  3. yummynath

    yummynath

    Joined:
    Jul 3, 2020
    Posts:
    19
    Issue resolved. There was an error in logic in another script in the child object
     
    Niborsom likes this.