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

Question Integer value from child to parent

Discussion in 'Scripting' started by nasargsyan, Aug 3, 2023.

  1. nasargsyan

    nasargsyan

    Joined:
    May 1, 2023
    Posts:
    15
    Hello there

    Here I have 2 scripts

    The Parent

    Code (CSharp):
    1. public class car_onoff : MonoBehaviour
    2. {
    3.        public GameObject BRman;
    4.        public int aval;
    5.  
    6.     void Update()
    7.     {
    8.      
    9.  
    10.         if(Input.GetKeyDown("z"))
    11.          {
    12.                 GetComponent<car_move1>().enabled = false;
    13.                 BRman.SetActive(true);
    14.                 BRman.GetComponent<charMove>().enabled = true;
    15.  
    16.                
    17.          }
    18.  
    19.          else if(Input.GetKeyDown("x") && aval == 2)
    20.          {
    21.                 GetComponent<car_move1>().enabled = true;
    22.                 BRman.SetActive(false);
    23.                 BRman.GetComponent<charMove>().enabled = false;
    24.                
    25.          }
    26.     }
    27. }
    For your information - when I press Z key the person should leave the car and control from car passes to person. When I press X , the person should get into the car IF it is on trigger area.



    Child code

    Code (CSharp):
    1. public class checkTr : MonoBehaviour
    2. {
    3.    
    4. public int aval;
    5.  
    6.     void OnTriggerEnter2D(Collider2D other)
    7.     {
    8.         if (other.gameObject.CompareTag("PersPlayer"))
    9.         {
    10.             aval = 2;
    11.         }
    12.     }
    13.  
    14.     void OnTriggerExit2D(Collider2D other)
    15.     {
    16.         if (other.gameObject.CompareTag("PersPlayer"))
    17.         {
    18.             aval = 3;
    19.         }
    20.     }
    21. }
    22.  

    Triggers work well, but don't pass the integer value of aval to Parent.
    I made this Parent/Child connection in order to move game objects and triggers together.

    The question is, how to pass the integer value from child to parent ?

    Thank you
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,362
    After the if statement you need to get the parent's component (with transform.parent.getcomponent or with transform.getcomponentinparent)
    Then you cah adjust the value of the parent
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    The same way as passing ANYTHING around:

    Referencing variables, fields, methods (anything non-static) in other script instances:

    https://forum.unity.com/threads/hel...-vars-in-another-script.1076825/#post-6944639

    https://forum.unity.com/threads/accessing-a-gameobject-in-different-scene.1103239/

    It isn't always the best idea for everything to access everything else all over the place. For instance, it is BAD for the player to reach into an enemy and reduce his health.

    Instead there should be a function you call on the enemy to reduce his health. All the same rules apply for the above steps: the function must be public AND you need a reference to the class instance.

    That way the enemy (and only the enemy) has code to reduce his health and simultaneously do anything else, such as kill him or make him reel from the impact, and all that code is centralized in one place.

    As you might have noticed DevDunk, I have had a huge turn away from these dynamic things in the last year... I have just seen too much cost and confusion come out of them, especially in a team environment where others may be manipulating your scenes / prefabs. But yes, you certainly could use Get- and Find- type constructs, I just think they are over-used, especially in tutorial scripts, and a ton of people end up here going "I got a nullref!"
     
  4. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,362
    Fair enough. I mostly work alone or am the only developer, so I can get away with a lot (;

    Definitely going to change when I get a job in a bit
     
    Kurt-Dekker likes this.