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

How to read a variable from one script to another but this variable might change in runtime

Discussion in 'Scripting' started by videoanime, Feb 9, 2015.

  1. videoanime

    videoanime

    Joined:
    Sep 28, 2014
    Posts:
    42
    Hi, I hope someone can explain to me how Unity works with this issue.

    I have 2 scripts, one in a prefab, and other in a gameobject in my hierarchy, I need these prefabs because they are clones that I make them to appear randomnly, so, the script in these clones is for move them, the script in my gameobject is for knowing when my clones touch the "floor", in this moment, I want to appear a new clone.

    The problem is that the variable that tells me when the clone touched the "floor" is false at the beginning and when it touches the "floor" change to true, but since my gameobject script, it always read it like false regardless it is true.

    The variable "floor" is public and is false at the beginning, and I change it in the Update function, but since my gameobject script it is always true.

    So, will there be a way that I can read that variable with its respective changes?

    I mean, what's the case to read variables from one script in a gameobject to other script in prefabs if I can't read it with the changes I need. For sure it should be a way, but I don't know it, can anyone help me?

    Thanks.
     
  2. meatpudding

    meatpudding

    Joined:
    Jan 28, 2015
    Posts:
    39
    It sounds like what you have should work. There may be some bug that is setting your variable to false when it should be true. It might be better to have a public property instead or a public variable. That way, you can set it up so you can still read the value from your other script, but you can only set it from the clone script.
     
  3. bear_love_honey

    bear_love_honey

    Joined:
    Jan 7, 2015
    Posts:
    31
    static variables dont help?

    public static bool TestVar;
     
  4. videoanime

    videoanime

    Joined:
    Sep 28, 2014
    Posts:
    42
    No, I put it static and I can't read it even it was public, I have to take the word "static".
     
  5. videoanime

    videoanime

    Joined:
    Sep 28, 2014
    Posts:
    42
    That's new for me, how do I set a public property? Now, you mention that might have a bug, I don't know, but check this, the clone script initialize the variable to false when a new clone is created, then, when this clone stops moving, the variable changes to true, this works fine until here. On the other hand, in my gameobject script, I'm always checking that variable in the next way:
    Code (CSharp):
    1. void Update()
    2.         {
    3.         floor = MP.suelo;
    4.         if (floor)
    5.             SD.Spawn ();
    6.         }
    And it is always false, by the way, I'm changing that variable to true in my clone script in FixedUpdate and I'm checking it in Update in my gameobject script, as you can see.
     
  6. meatpudding

    meatpudding

    Joined:
    Jan 28, 2015
    Posts:
    39
    To use a property for example
    Code (csharp):
    1.  public float Suelo {
    2.   get { return suelo; }
    3. }
    In this way you can only read the value from your other script, but can't set it. Not that it is case sensitive and make suelo private or protected.
     
  7. videoanime

    videoanime

    Joined:
    Sep 28, 2014
    Posts:
    42
    I update this, I declare my variable like this
    Code (CSharp):
    1. public static bool suelo;
    And now I can read first in false and after in true, thank a lots for your suggestions :D
     
  8. meatpudding

    meatpudding

    Joined:
    Jan 28, 2015
    Posts:
    39
    If the variable is static, then you have the same value for every instance...
     
    videoanime likes this.