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

Accessing two different scripts

Discussion in 'Scripting' started by edu.silveira, Jun 4, 2011.

  1. edu.silveira

    edu.silveira

    Joined:
    Apr 26, 2011
    Posts:
    13
    Hello !

    Guys, please, I need your help. I have 3 GameObjects in the scene:

    -the !GameMaster object, which contains the script "globalVariables"
    -the !GameMaster1 object, which contains the script "rayCastCollide"
    -the !GameMaster2 object, which contains the script "rayCastCollide2"

    *globalVariables' Update contains a code to check whether if two different booleans are true or not.

    *rayCastCollide generates a Ray that, when collides with a box collider tagged "Solution1", will access the globalVariables script and change its boolean to true.

    *rayCastCollide2 generates a different Ray from a different position, which will access and change the same bools in the globalVariables script.

    The problem I'm having is: The 2 scripts detect the collisions fine, but, when both are active, only one will successfully change the globalVariables' boolean.

    For example: when rayCastCollide1 and 2 are active, only rayCast1 will change the boolean. But if I deactivate the rayCast1, rayCast2 will successfully change the boolean.

    The code which generates the ray is inside an Update function, in both scripts. If, for example, the code in rayCast1 is inside a FixedUpdate function, and the code in rayCast2 is inside an Update function, then only the rayCast2 will access the GlobalVariables.

    tl;dr: 2 different scripts trying to access and change another script's value at the same time, but only one is able to do so. Is there a way to fix this?

    Thanks !
     
  2. NomadKing

    NomadKing

    Joined:
    Feb 11, 2010
    Posts:
    1,461
    Do the scripts only access the globalVariables to change the boolean to true? or do they set false as well?
     
  3. edu.silveira

    edu.silveira

    Joined:
    Apr 26, 2011
    Posts:
    13
    Nevermind, I just fixed it !

    NomadKing, yes, and that was the problem. Each ray had an "else { cristal1.Cristal1Activated = false } , so when one wasn't colliding, it would always set the bool to false and not let the other change it to true.
     
  4. NomadKing

    NomadKing

    Joined:
    Feb 11, 2010
    Posts:
    1,461
    I was going to suggest that was what it was. Glad it's working now :)