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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Variable not updating...

Discussion in 'Scripting' started by fromoldcity, Mar 24, 2021.

  1. fromoldcity

    fromoldcity

    Joined:
    May 17, 2019
    Posts:
    7
    Hi
    Noob Question. I cant understand why my variables not updating in runtime situation. When Camera starts to zoom my triggering box collider's size copy this zoom. But my first code doesnt updating. Second one is working but very expensive I think(Getcomponents in update() ). What is the problem??

    -This is Broken Code-

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class DenemeBoxCam : MonoBehaviour
    7. {
    8.     private Vector2 m_Size;
    9.     private Vector2 b_Size;
    10.     private float boxX;
    11.     private float boxY;
    12.  
    13.     void Awake()
    14.     {
    15.         m_Size = this.gameObject.GetComponent<BoxCollider2D>().size;
    16.         boxX = this.gameObject.GetComponent<Camera>().orthographicSize;
    17.         boxY = this.gameObject.GetComponent<Camera>().orthographicSize;
    18.         b_Size= this.gameObject.GetComponent<BoxCollider2D>().size;
    19.     }
    20.    
    21.     void Update()
    22.     {
    23.  
    24.         m_Size = new Vector2 (boxX*4.1f,boxY*1.9f);
    25.         b_Size= m_Size;
    26.         Debug.Log(m_Size);
    27.         Debug.Log(b_Size);
    28.      
    29.     }
    30. }
    31.  
    -This is Working Code-

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DenemeBoxCam : MonoBehaviour
    6. {
    7.     private Vector2 m_Size;
    8.     private float boxX;
    9.     private float boxY;
    10.  
    11.     void Start()
    12.     {
    13.         m_Size = this.gameObject.GetComponent<BoxCollider2D>().size;
    14.      
    15.     }
    16.    
    17.     void Update()
    18.     {
    19.         boxX = this.gameObject.GetComponent<Camera>().orthographicSize * 4.1f;
    20.         boxY = this.gameObject.GetComponent<Camera>().orthographicSize * 1.9f;
    21.         m_Size = new Vector2 (boxX,boxY);
    22.         this.gameObject.GetComponent<BoxCollider2D>().size = m_Size;
    23.      
    24.     }
    25. }
    26.  
     
  2. Putcho

    Putcho

    Joined:
    Jun 1, 2013
    Posts:
    246
    your first script everything else fine but what you did wrong is that your b_Size is a vector2 not a BoxCollider2D Class(Component), your b_Size get update but your BoxCollider2D did not because you didn't referent it

    this is your first script should look like
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class DenemeBoxCam : MonoBehaviour
    5. {
    6.     private Vector2 m_Size;
    7.     private BoxCollider2D b2D;
    8.     private float boxX;
    9.     private float boxY;
    10.  
    11.     void Awake()
    12.     {
    13.         //no need "this.gameObject"  because MonoBehaviour "gameObject" is already part of this class
    14.         boxX = gameObject.GetComponent<Camera>().orthographicSize;
    15.         boxY = gameObject.GetComponent<Camera>().orthographicSize;
    16.         b2D = gameObject.GetComponent<BoxCollider2D>();
    17.         m_Size = b2D.size;
    18.     }
    19.  
    20.     void Update()
    21.     {
    22.  
    23.         m_Size = new Vector2(boxX * 4.1f, boxY * 1.9f);
    24.         b2D.size = m_Size;
    25.         Debug.Log(m_Size);
    26.         Debug.Log(b2D.size);
    27.     }
    28. }
     
  3. Putcho

    Putcho

    Joined:
    Jun 1, 2013
    Posts:
    246
    context
     
  4. fromoldcity

    fromoldcity

    Joined:
    May 17, 2019
    Posts:
    7
    Thank you Putcho. It helped a lot. By the way I run your code again and I faced same problem. My camera orthographicSize is going to bigger when zoom in and out but my boxcollider2D size is not updating. You see Debug.Log's in update. They are - same as b2D.size and m_size variables- not updating with orthographicSize. My only working situation is in update() work. Like as boxX = GetComponent<Camera>().orthographicSize; in Update(). But its too cost. Is there a way to do this or Am I wrong to do something?
    Thats I am trying to say in code. This is working but I use something like that.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DenemeBoxCam : MonoBehaviour
    6. {
    7.     private Vector2 m_Size;
    8.     private BoxCollider2D b2D;
    9.     private float boxX;
    10.     private float boxY;
    11.  
    12.     void Awake()
    13.    
    14.     {
    15.         boxX=gameObject.GetComponent<Camera>().orthographicSize;
    16.         boxY=gameObject.GetComponent<Camera>().orthographicSize;
    17.         b2D=gameObject.GetComponent<BoxCollider2D>();
    18.         m_Size=b2D.size;
    19.        
    20.        
    21.     }
    22.     void Update()
    23.     {
    24.        
    25.         m_Size = new Vector2 (gameObject.GetComponent<Camera>().orthographicSize*4.1f,GetComponent<Camera>().orthographicSize*1.9f);
    26.         b2D.size=m_Size;
    27.         Debug.Log("msize"+m_Size);
    28.         Debug.Log("b2D"+b2D.size);
    29.     }
    30. }
    31.  
     
    Last edited: Mar 26, 2021
  5. Putcho

    Putcho

    Joined:
    Jun 1, 2013
    Posts:
    246
    so your boxX and boxY are dynamic as well, if you understand how we just fix BoxCollider2D
    we do the same thing to Camera

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class DenemeBoxCam : MonoBehaviour
    5. {
    6.     private Vector2 m_Size;
    7.     private BoxCollider2D b2D;
    8.     private Camera cam;
    9.  
    10.     void Awake()
    11.  
    12.     {
    13.         cam = GetComponent<Camera>();
    14.         b2D = GetComponent<BoxCollider2D>();
    15.    
    16.         m_Size = b2D.size;
    17.     }
    18.     void Update()
    19.     {
    20.         var orthographicSize = cam.orthographicSize;
    21.  
    22.         m_Size = new Vector2(orthographicSize * 4.1f, orthographicSize * 1.9f);
    23.         b2D.size = m_Size;
    24.  
    25.         Debug.Log("msize" + m_Size);
    26.         Debug.Log("b2D" + b2D.size);
    27.     }
    28. }
    by the way use "reply" on my post, else I wont get notify if you editing something
     
    Last edited: Mar 26, 2021
  6. Putcho

    Putcho

    Joined:
    Jun 1, 2013
    Posts:
    246
    if you want to update collider size every frame use the code above
    if you want to update collider size only when camera.orthographicSize changed
    try this one
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class DenemeBoxCam : MonoBehaviour
    5. {
    6.     private Vector2 m_Size;
    7.     private BoxCollider2D b2D;
    8.     private Camera cam;
    9.  
    10.     private float currentOrthographicSize;
    11.  
    12.     void Awake()
    13.     {
    14.         cam = GetComponent<Camera>();
    15.         b2D = GetComponent<BoxCollider2D>();
    16.  
    17.         ChangeBoxCollider2DSize();
    18.     }
    19.     void Update()
    20.     {
    21.         //if cam.orthographicSize == currentOrthographicSize skip code below
    22.         if (cam.orthographicSize == currentOrthographicSize)
    23.             return;
    24.  
    25.         //only update b2D.size when cam.orthographicSize changed
    26.         ChangeBoxCollider2DSize();
    27.     }
    28.  
    29.     private void ChangeBoxCollider2DSize()
    30.     {
    31.         currentOrthographicSize = cam.orthographicSize;
    32.         m_Size = new Vector2(currentOrthographicSize * 4.1f, currentOrthographicSize * 1.9f);
    33.         b2D.size = m_Size;
    34.  
    35.         //remove these debug.log once you done using it
    36.         Debug.Log("currentOrthographicSize" + currentOrthographicSize);
    37.         Debug.Log("msize" + m_Size);
    38.         Debug.Log("b2D" + b2D.size);
    39.         Debug.Log("updated");
    40.     }
    41. }
     
  7. fromoldcity

    fromoldcity

    Joined:
    May 17, 2019
    Posts:
    7
    Finally, I got how its works. I'm gonna use your second code. And I have to edit my other scripts like this.