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

Passing Variables Between Scripts

Discussion in 'Scripting' started by JayArtist, Jan 28, 2022.

  1. JayArtist

    JayArtist

    Joined:
    Jul 1, 2014
    Posts:
    89
    Hi,

    I'm having trouble understanding how variables are shared between scripts. I modified a snippet of code from a tutorial which works and allows me to pass a bool variable from a Ladder.cs script to my PlayerController.cs script, but I need to understand it more as I have other variables, such as a Transform that I'd like to share from the ladder script to the Player's script.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Ladder : MonoBehaviour
    6. {
    7.  
    8.     public Transform ladderCentre; // The ladders centre which I also wish to share
    9.     private PlayerController thePlayer; // Create a variable to store the Player's Object and Components
    10.  
    11.     void Start()
    12.     {
    13.         // Find the Player's Game Object and Script - Store in thePlayer variable
    14.         thePlayer = FindObjectOfType<PlayerController>();
    15.     }
    16.  
    17.  
    18.     // Check if Touching the Ladder
    19.     void OnTriggerEnter2D(Collider2D other)
    20.     {
    21.         if(other.name == "Player")
    22.         {
    23.             thePlayer.onLadder = true;
    24.         }
    25.     }
    26.  
    27.     // Check if Leaving the Ladder
    28.     void OnTriggerExit2D(Collider2D other)
    29.     {
    30.         if(other.name == "Player")
    31.         {
    32.             thePlayer.onLadder = false;
    33.         }
    34.     }
    35. }
    36.  
    Then in the Player's script I have:

    Code (CSharp):
    1. public bool onLadder = false; // Bool Variable shared with Ladder Script

    In the Ladder script it appears to use a reserved keyword "PlayerController" - which I think is the actual script name?? although I couldn't find information on this online - this initially is what is confusing me.

    I search for the PlayerController script and store it in thePlayer variable, but this seems back-to-front - again what's going on?

    Also when the bool already exists, I don't understand why I then have to re-create the bool variable in the Player's script for it to be accessable.

    I'm totally confused!

    Could someone kindly explain what is going on please, very slowly, as I feel pretty dumb just now :(

    Thanks.
     
  2. JayArtist

    JayArtist

    Joined:
    Jul 1, 2014
    Posts:
    89
    I think I get it now, but thanks anyway ;)
     
  3. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    It would appear that PlayerController is a MonoBehaviour class. Meaning someone wrote it and now it is attached to an object in the hierarchy. Ladder is holding a reference to it; finding it at Start and then able to access any parts set to public. Basically you just know its location in space and time so now you can communicate with it.

    thePlayer is just what you named the reference to the PlayerController you found at Start. thePlayer is now PlayerController for which ever object you found it on during Start. Maybe be less confusing to find the object in some other way specifically like by name so you know which object it is on just by reading the code.

    You did not recreate a bool in this instance but created a reference to PlayerController and since that bool is public you can read and write to it.
     
    Last edited: Jan 28, 2022