Search Unity

Accessing a variable in one script from another script? (using C#)

Discussion in 'Scripting' started by rhianu, Feb 11, 2011.

  1. rhianu

    rhianu

    Joined:
    Feb 25, 2010
    Posts:
    93
    Okay, I've been trying to figure this out forever but it's not working and it's driving me crazy.

    I've got two game objects, and I need the second game object to do something when the player collides with the first game object.

    The first game object, which works exactly like it's supposed to, is just a red cube that I've set up to detect collisions from the player using the following script:

    collision.cs
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class collision : MonoBehaviour
    5. {
    6.     public static bool touchedRedCube = false;
    7.     int i = 0;
    8.  
    9.     void OnTriggerEnter(Collider other)
    10.     {
    11.         if (other.gameObject.tag == "Player")
    12.         {
    13.             i++;
    14.             Debug.Log("Touched the cube " + i);
    15.             touchedRedCube = true;
    16.         }
    17.     }
    18. }
    Now like I said, that above code works just fine. The problem comes in when I try to have another game object preform an action when the player collides with the red cube. This other game object is just an EmptyGameObject with the following script attached:

    test.cs
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class test : MonoBehaviour
    5. {
    6.  
    7.     // Use this for initialization
    8.     void Start ()
    9.     {
    10.    
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update ()
    15.     {
    16.         if (collision.touchedRedCube == true);
    17.         {
    18.             Debug.Log("Preformed an action.");
    19.         }
    20.     }
    21. }
    However, instead of printing "Preformed an action" to the debug log only when the player touches the red cube like it should, the above script starts printing the message as soon as the level loads, and continually prints it every single frame.

    What am I doing wrong?
     
    Last edited: Feb 11, 2011
  2. Cameron_SM

    Cameron_SM

    Joined:
    Jun 1, 2009
    Posts:
    915
    Hard to read your code with those kind of naming conventions :-|

    However, this may be because once you set the static class field touchedStarsDown to true it's never set to false again. If the player spawns while colliding with the trigger it'll be set true and stay that way until you specifically set it back to false or exit the game.

    If you just want a one-off event to happen in a different MonoBehaviour, look in the scripting manual for SendMessage as you don't have to worry so much about managing the state of flag variables like you are here.
     
  3. rhianu

    rhianu

    Joined:
    Feb 25, 2010
    Posts:
    93
    Yes, I'm aware that the message would continually print after the player collides with the cube, but I specifically set up the scene so the player doesn't spawn on top of it.
     
  4. Cameron_SM

    Cameron_SM

    Joined:
    Jun 1, 2009
    Posts:
    915
    Well I can't see any other reason why it would do that, but then again I've never use static variables that way.

    Have you tried stepping through it with the debugger?
     
  5. rhianu

    rhianu

    Joined:
    Feb 25, 2010
    Posts:
    93
    All I've used is Unity's Error Console. Is there something else I should be using?
     
  6. rhianu

    rhianu

    Joined:
    Feb 25, 2010
    Posts:
    93
    I've changed the naming conventions in the script so it should be easier to follow logically.
     
  7. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    You can debug using MonoDevelop (I think we were talking about this in your other thread).

    Regarding your problem, I've been assuming that you're just starting playback on a scene, and the message immediately start printing. Is that the case, or is this happening when you load a new scene using Application.LoadLevel()?

    If the latter, keep in mind that static variables won't be reset to their initial values when a new level is loaded, so if the variable was 'true' in the previous scene, it'll be 'true' in the newly loaded scene as well.
     
  8. rhianu

    rhianu

    Joined:
    Feb 25, 2010
    Posts:
    93
    I asked this question on UnityAnswers, and finally managed to get the answer I needed. Turns out I just needed to set the variable inside an Awake() function:

    http://answers.unity3d.com/question...ble-in-one-script-from-another-script-using-c

    Also, this part of the code:

    Code (csharp):
    1. if (collision.touchedRedCube == true);
    2. {
    3.   Debug.Log("Preformed an action.");
    4. }
    ...should have looked like this:

    Code (csharp):
    1. if (collision.touchedRedCube)
    2. {
    3.   Debug.Log("Preformed an action.");
    4. }
     
    Last edited: Feb 13, 2011
  9. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    I know you already solved the problem, but if I may ask, was this happening when switching from one level to another using Application.LoadLevel()? Or was this happening on the first level loaded after entering play mode in the editor?
     
  10. rhianu

    rhianu

    Joined:
    Feb 25, 2010
    Posts:
    93
    ^Both. It didn't seem to make any difference whether I started in another scene or not.