- Home /
Get Boolean from another Script, C#
I have already tried to ask this question befor, but let me try aggain:
I want to know an easy way to use a boolean that is toggled in "Script1" in "Script2" (in c#)
This Question was also already asked a 100 times by other people but I just don't seem to understand...It would be great if someone could help me,
Thank you
Not in C# but it should be easy to convert the three lines of code: http://codeparalysis.blogspot.co.uk/2013/12/how-to-access-variable-in-script-from.html
Answer by YoungDeveloper · Dec 15, 2013 at 07:20 PM
Hey there, here is a detailed explanation how access stuff from other script! If you still dont understand specific detailed, sure ask.
http://answers.unity3d.com/questions/550578/cant-understand-getcomponent-c.html
Thank You that made some things clear :) But still I seem to unskilled to fix my problem on my own...
I want a door to open when my Player (just a ball) collides with a "switch". $$anonymous$$y Player is taged "$$anonymous$$ugel"
The Switch script:
public class Switch : $$anonymous$$onoBehaviour {
public bool tuerAuf;
// Use this for initialization
void Start () {
tuerAuf = false;
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter (Collision collision) //=======================================================================================================================================
{
if (collision.gameObject.tag == "$$anonymous$$ugel")
{
tuerAuf = true;
}
}
}
The Door Script:
public class TuerSwitch : $$anonymous$$onoBehaviour { private Vector3 startPos; public bool auf;
public GameObject CollisionCube;
Switch switchScript;
// Use this for initialization
void Start () {
startPos = transform.position;
auf = false;
switchScript = CollisionCube.GetComponent<Switch> ();
auf = switchScript.tuerAuf;
}
// Update is called once per frame
void Update ()
{
if(auf)
{
transform.Translate (0, 0, 3 * Time.deltaTime);
if (transform.position.z > startPos.z + 1)
auf = false;
}
}
}
It would be really great if you could help me solve my Problem :D The door just won't move
Ins$$anonymous$$d of checking the collision on door, you should do it on the player ins$$anonymous$$d, because what if you will have 10 doors, it means there will 10 on collision checks each frame. But as it only will be on player, there will be only one oncollision check.
Still is all what you need on player:
void OnCollisionEnter(Collision col){
if (col.gameObject.tag == "Door"){
Switch switchScript = col.gameObject.GetComponent<Switch>(); //Get the Switch script from collision gameobject col
switchScript.OpenDoor(); //runs the function to open door
}
}
This should be on door: (You must have tag "Door" set)
public class Switch : $$anonymous$$onoBehaviour {
private bool open = false; //boolean to trigger opening
public void OpenDoor(){
open = true;
}
void Update(){
if(open){
//open door code
}
}
}
I think I now have the exact same code implemented, but it still doesn't work... I am getting slightly frustrated over this
I just recreated the Object with "Door" on it, and now it works :) Thanks a lot you made my day
Answer by Spinnernicholas · Dec 15, 2013 at 07:32 PM
If they are on the same gameobject:
//in script1
public boolean toggle
//in script2
if(getComponent<script1>().toggle)
{
}
If they are not on the same GameObject:
//in script1
public boolean toggle
//in script2
//Set this to the other script in the Unity Editor
public script1 otherScript
//...
if(otherScript.toggle)
{
}
There are many other ways if they are on different gameObjects.
I might just be to stupid, but it still doesn't work... I now get NullReferenceException: Object reference not set to an instance of an object
In script 2 it now looks like this:
if (otherScript.toggle)
auf = true;
if(auf)
{
transform.Translate (0, 0, 3 * Time.deltaTime);
if (transform.position.z > startPos.z + 1)
auf = false;
}