Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug functions being called erratically from if cases in update

Discussion in 'C# Job System' started by alice_balls, Aug 26, 2023.

  1. alice_balls

    alice_balls

    Joined:
    Apr 12, 2020
    Posts:
    4
    i have a script that moves the camera a set amount when the player exceeds a certain distance from said camera in a given direction
    i use an event to indicate when the camera moves to the next screen
    however, even if the camera doesn't move, the functions still are called constantly
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.Events;
    6. public class CameraController : MonoBehaviour
    7. {
    8.     GameObject player;
    9.     Vector2 playerRelative;
    10.     public float northBoundary, southBoundary, eastBoundary, westBoundary;
    11.     public Vector2 cameraMoveAmount;
    12.  
    13.     public static event Action OnCameraMove;
    14.     void Start()
    15.     {
    16.         if(GameObject.FindGameObjectWithTag("player")) player = GameObject.FindGameObjectWithTag("player");
    17.    
    18.     }
    19.     void Update()
    20.     {
    21.         if(!player)
    22.         {
    23.             if (GameObject.FindGameObjectWithTag("player")) player = GameObject.FindGameObjectWithTag("payer");
    24.         }
    25.         if(player)
    26.         {
    27.             playerRelative = - transform.position + player.transform.position;
    28.             if(playerRelative.x < westBoundary)
    29.             {
    30.                 Debug.Log(playerRelative);
    31.                 transform.position -= new Vector3(cameraMoveAmount.x, 0, 0);
    32.                 //ScreenFlip();
    33.             }
    34.             if (playerRelative.x > eastBoundary)
    35.             {
    36.                 Debug.Log(playerRelative);
    37.                 transform.position += new Vector3(cameraMoveAmount.x, 0, 0);
    38.                 //ScreenFlip();
    39.             }
    40.             if (playerRelative.y < southBoundary)
    41.             {
    42.                 Debug.Log(playerRelative);
    43.                 transform.position -= new Vector3(0, cameraMoveAmount.y, 0);
    44.                 //ScreenFlip();
    45.             }
    46.             if (playerRelative.y > northBoundary)
    47.             {
    48.                 Debug.Log(playerRelative);
    49.                 transform.position += new Vector3(0, cameraMoveAmount.y, 0);
    50.                 //ScreenFlip();
    51.             }
    52.         }
    53.     }
    54.  
    55.     void ScreenFlip()
    56.     {
    57.         //OnCameraMove.Invoke();
    58.         Debug.Log("screen flip invoked");
    59.     }
    60. }
    61.  


    as u can probably see, even tho the parameters within the if loops are not met, the enclosed functions still get called every cycle.

    i am a very very very amateur programmer, and also a bit of a dumbass, so im sorry if this question is worded poorly or has an obvious and simple answer

    thank u
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,721
    Incorrect. When the code inside an if block runs, it means the condition of the corresponding if clause evaluates to true. You just assume that the conditions aren't met - but obviously they are. ;)

    Hint: learn to use the debugger. Set a breakpoint, attach debugger, enter playmode, step over code, check the values of variables.

    ^^ and fix that typo
     
  3. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    19,968
    Line 23 has a typo and is an excellent example of why I hate the
    GameObject.Find
    methods. They're prone to errors thanks to the compiler not being able to identify problems in the string. It's one of those things that you should try to get away from as soon as possible. They're just too much of a headache.

    For now while you're learning you can just have a check directly below them that errors if it fails. I've also removed the
    if
    statement as there was no point to it. The
    this
    in the log command makes it possible to double click the message in the console to select the object it occurs for.
    Code (csharp):
    1. player = GameObject.FindGameObjectWithTag("player");
    2. if (player == null) Debug.LogWarning("Failed to find player.", this);
     
    Last edited: Aug 27, 2023
  4. alice_balls

    alice_balls

    Joined:
    Apr 12, 2020
    Posts:
    4
    thank u for the help, i think this will work nicely