Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Calling object from another script?

Discussion in 'Scripting' started by leapdwang, Jun 9, 2020.

  1. leapdwang

    leapdwang

    Joined:
    Apr 1, 2020
    Posts:
    13
    I need help accessing a certain int from another script, but when being called my script doesn't retain the original value of the int from its respective script, rather the variable defaults to 0. The variable also only reduces to -1, rather than continually being reduced as new triggers enter the object.

    Code (CSharp):
    1. public class CashierPick : MonoBehaviour
    2. {
    3.     //Creates private variables in the class
    4.     public GameObject levelIntializer;
    5.     public GameObject money;
    6.     GameObject cashier;
    7.  
    8.     Transform rightHand;
    9.     Transform leftHand;
    10.  
    11.     //GameObject Initializer;
    12.     LevelInitializer lvlIntial;
    13.  
    14.     int items;
    15.     float time;
    16.  
    17.     private void Start()
    18.     {
    19.         //Assigns GameObjects to their proper objects in Unity
    20.         cashier = GameObject.Find("Cashier");
    21.        
    22.         //0 = Right Hand, 1 = Left Hand
    23.         rightHand = cashier.transform.GetChild(0);
    24.         leftHand = cashier.transform.GetChild(1);
    25.  
    26.         //Locates Money Object for player to give appropriate money to cashier, sets it invisible to the player
    27.         money = GameObject.Find("Money");
    28.         money.SetActive(false);
    29.  
    30.         //Gets items variable from Level Intializer script
    31.         lvlIntial = levelIntializer.GetComponent<LevelInitializer>();
    32.         items = lvlIntial.items;
    33.  
    34.         Debug.Log(items);
    35.     }
    36.  
    37.     private void OnTriggerEnter(Collider other)
    38.     {
    39.         //Teleports scanned object to cashier's Right Hand (will change!)
    40.         if (this.transform.parent == null && other.tag == "Scanner")
    41.         {
    42.             this.transform.parent = cashier.transform;
    43.             this.transform.position = rightHand.transform.position;
    44.  
    45.             items--;
    46.             Debug.Log("Collide!");
    47.             Debug.Log(items);
    48.  
    49.             //If objects are all bagged, reveals money for player to give cashier
    50.             if (items == -1)
    51.             {
    52.                 money.SetActive(true);
    53.             }
    54.         }
    55.     }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
    You can delete this whole line as it does nothing:
    Code (CSharp):
    1. lvlIntial = levelIntializer.GetComponent<LevelInitializer>();
    What logs does your game print when new items collide? Are you sure your OnTriggerEnter is running at all?
     
  3. leapdwang

    leapdwang

    Joined:
    Apr 1, 2020
    Posts:
    13
    OnTriggerEnter does work, how would you suggest I get a certain variable from a different script.
     
  4. leapdwang

    leapdwang

    Joined:
    Apr 1, 2020
    Posts:
    13
    But what happens is say the "items--;", the consol log will only show "-1" every time there is OnTriggerEnter and so that's the value of the variable.
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
    There's nothing wrong with the way you're getting the value from your other script. The question is what exactly is happening and how is it not matching your expectations? You said the value isn't going lower than -1. Where is it starting? What does your script print in Start()?
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
    Ok. Are you sure you only have one copy of this script in your scene? What happens if you add this in OnTriggerEnter()?

    Code (CSharp):
    1. Debug.Log($"My name is {this.name} and my id is {GetInstanceId()}");
    Does it print the same instance id each time or a different one?
     
  7. leapdwang

    leapdwang

    Joined:
    Apr 1, 2020
    Posts:
    13
    It gives me this error message in Visual Studio:

    Error CS0103 The name 'GetInstanceId' does not exist in the current
     
  8. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
    Sorry it's "GetInstanceID". Capital D at the end.
     
  9. leapdwang

    leapdwang

    Joined:
    Apr 1, 2020
    Posts:
    13
    I get a different ID instance each time, even though they are the same prefab.
     
  10. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
    Every time you Instantiate a prefab you get a whole new object with it's own variables and everything.
     
  11. leapdwang

    leapdwang

    Joined:
    Apr 1, 2020
    Posts:
    13
    Sounds good, so the trigger function works, but I still have trouble changing a certain variable from another script.
     
  12. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
    Your problem is that you don't actually have a centralized variable at all that you are trying to change. You're just changing a local variable that exists on each of your new instances. You need a long-lived object that will keep track of this value, across the lifespan of many of your CashierPick objects. What's the end goal here?

    Do you want to change the value of the variable from your LevelInitializer?
     
  13. leapdwang

    leapdwang

    Joined:
    Apr 1, 2020
    Posts:
    13
    Yes, I want to lower the variable by 1 every time a trigger enters a certain object, and when the variable gets to 0, it'll make a certain game object active.

    Code (CSharp):
    1. if(items==0){
    2.      money.SetActive(true);
    3. }
     
  14. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
    Have you tried this?
    Code (CSharp):
    1. lvlInitial.items--;
    Code (CSharp):
    1. if(lvlInitial.items==0){
     
  15. leapdwang

    leapdwang

    Joined:
    Apr 1, 2020
    Posts:
    13
    Thank you, I kind of got it working, but there are still issues thank you so much for the help!