- Home /
Activate public bool from other script
Hello i have a problem to activate a public bool member in other script file in GameObject.
I Have a gameobject called "Elevatore" (Elevator)
whit this script and public bool member
http://img838.imageshack.us/img838/7158/objws1.png
Elevat Script
Discesa [ ] (Bool member) Salita [ ] (Bool member)
now in other object whit an'other script i need to enabile this bool
example
void OnTriggerEnter(Collider object) {
object.Discesa = true;
}
How to?
check fafases answer here : http://answers.unity3d.com/questions/368362/accessing-variables-from-different-scripts.html
check Fatties answer here : http://answers.unity3d.com/questions/332250/accessing-other-gameobjects-script-variables.html
Answer by DanielJF · Jan 08, 2013 at 12:36 PM
I will try to explain the basics because your question is kinda confusing but i hope this helps.
class TestFile extends MonoBehaviour
{
static var main : TestFile;
var Discesa : boolean = true;
function Awake()
{
main = this;
}
}
in this first class i create a boolean called Discesa and it's true in the begin. Now i will make a second script and put the boolean to false.
class testfile2 extends MonoBehaviour
{
function Start ()
{
TestFile.main.Discesa = false;
Debug.Log(TestFile.main.Discesa);
}
}
by creating a var called main in the first script it will be able to find this script. the thing you do first is type the name off the class then main so the class knows you mean this script. Now you are in the script and you can acces all functions and variables etc.. //script1 using UnityEngine; using System.Collections;
public class TestFile1 : MonoBehaviour {
public bool Discesa = true;
public static TestFile1 main;
void Awake ()
{
main = this;
}
}
//script 2
using UnityEngine;
using System.Collections;
public class TestFile2 : MonoBehaviour {
// Use this for initialization
void Start ()
{
TestFile1.main.Discesa = false;
}
}
Hope it helped.
i just see you program in c# i will update my post in a sec for c# im not that well with it but i think i can fix it
tanks $$anonymous$$ :)
Your code i think is good boy for my situation does work. i now have try this
void OnTriggerEnter(Collider coll) {
coll.gameObject.GetComponent().discesa=true;
}
Unity say: NullReferenceException: Object reference not set to an instance of an object pulsante.OnTriggerEnter (UnityEngine.Collider coll) (at Assets/pulsante.cs:24)
:/
using UnityEngine;
using System.Collections;
public class TestFile1 : $$anonymous$$onoBehaviour {
public bool Discesa = true;
public static TestFile1 main;
void Awake ()
{
main = this;
}
}
using UnityEngine;
using System.Collections;
public class TestFile2 : $$anonymous$$onoBehaviour {
public bool Test;
void Start ()
{
Test = gameObject.GetComponent<TestFile1>();
TestFile1.main.Discesa = false;
}
}
$$anonymous$$aybe this helps for you.
i Have follow your example but nothing error. here my script:
File: pulsante.cs
This script is placed on a Cube this cube is an Activator. If player touch this cube. this script enable a bool contains in other script for running elevator. called elvat.cs
using UnityEngine; using System.Collections;
public class pulsante : $$anonymous$$onoBehaviour {
// Use this for initialization
void Start ()
{
}
void OnTriggerEnter(Collider coll)
{
print ("touch");
var Test = gameObject.GetComponent();
Test.main.main.Discesa = false;
}
// Update is called once per frame
void Update () {
}
}
This script is placed on simple cube for create a elevator on a object. if player touch the object the script need to turn on a bool discesa or salita true | false.
File Elvat.cs
This is a Elevator script. if discesa bool is enabled, The script move a gameobject containts this script up or down. salita = up discesa = down
using UnityEngine; using System.Collections;
public class elvat : $$anonymous$$onoBehaviour {
public static elvat main;
// Use this for initialization
void Awake ()
{
main = this;
}
// Update is called once per frame
public bool discesa = false;
public bool salita = false;
void Update () {
if(discesa)
{
float velocity = 1f;
float TimedeltaTime = Time.deltaTime;
transform.position = new Vector3(transform.position.x,transform.position.y - velocity * TimedeltaTime,transform.position.z);
if(transform.position.y >= 70)velocity = -5;
else if(transform.position.y <= 40)velocity = 10;
}
if(salita)
{
float velocity = 1f;
float TimedeltaTime = Time.deltaTime;
transform.position = new Vector3(transform.position.x,transform.position.y + velocity * TimedeltaTime,transform.position.z);
if(transform.position.y >= 70)velocity = +5;
else if(transform.position.y <= 40)velocity = 10;
}
}
void OnTriggerEnter(Collider other)
{
}
}
Who are the problem?
Fixed Sorry
GameObject obj = GameObject.FindWithTag ("Elevatore"); print (obj.transform.position.x.ToString()); obj.GetComponent().discesa=true;
Your answer
![](https://koobas.hobune.stream/wayback/20220613090602im_/https://answers.unity.com/themes/thub/images/avi.jpg)