简体   繁体   中英

Unity sharing Boolean between object scripts

I was trying to create a puzzle where a box when moved onto a pressure plate opens a gate. When I get the box onto the pressure plate I was trying to pass a Boolean to the gate telling it to open. Is this the correct way of achieving my goals?

I have unity version 3.4 it is not pro and i'm coding in C#.

Any help appreciated as I have just started trying to learn unity.

Let's say your gate game object has the name MagicGate in your hierarchy view and has a script component assigned of type GateController containing the following code:

public class GateController : MonoBehaviour {
    // Awake(), Update(), ...
    public void OpenGate ();

    public void OpenGate () {
      // code for opening the gate
    }

There is another class maybe GameStatusController where you detect that the box is at the magic pressure plate (Colliders are useful for this). There you do:

GameObject player = GameObject.Find ("MagicGate");
GateController gateController = player.GetComponent<GateController> ();
gateController.OpenGate ();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM