- Home /
How to activate another Object with java script?
I have a GUIButton and a Object, name: "Car". I will activate the car with the GUIButton. GUIButton have this script:
function OnGUI()
{
GUI.enabled = true;
if(GUI.Button(new Rect(0, 0, 200, 100),"Touch here to activate Car."))
{
gameObject.active = false;
}
}
I know the "gameObject.active" it's not good here, but how can activate my "Car" Object?
I´ve the same problem. And I´m sorry I´m a newbie too so I don´t have an answer. And sorry for my english I´m a german pupil :D
Answer by syclamoth · May 13, 2012 at 10:49 PM
Have at the top of your script:
public var car : GameObject;
this will create a slot in the script inspector that you can drag the car onto. Then,
car.active = false;
will deactivate the car!
Answer by theabsinthehare · May 13, 2012 at 11:03 PM
By "activate" do you mean enable/disable the entire object, or do you want to access a function on the object "Car"?
If you want to access a function, you would reference the car via a GameObject variable in the script with the GUI function, then use GetComponent to access the script and function you want.
private var carVariable : GameObject;
function Start ()
{
carVariable = GameObject.Find("Car")
}
function OnGUI()
{
GUI.enabled = true;
if(GUI.Button(new Rect(0, 0, 200, 100),"Touch here to activate Car."))
{
carVariable.GetComponent(NameOfScriptOnCar).NameOfFunction(param1, param2, etc);
}
}
But don't use GameObject.Find(string), there's never a reason to do that except for laziness.
Answer by bompi88 · May 13, 2012 at 11:01 PM
An example. Drag your Car object to the car placeholder in the inspector.
var Car : GameObject;
// this should probably be in your car script or somewhere else,
// I don't know what you actually are trying to do here.
function Awake () {
Car.active = false;
}
function OnGUI()
{
GUI.enabled = true;
if(GUI.Button(new Rect(0, 0, 200, 100),"Touch here to activate Car."))
{
gameObject.active = true;
}
}
Answer by gordeszkakerek · May 16, 2012 at 04:13 PM
Thanks for all answer. Unfortunately it's not work for me. Maybe I wrote someting bad in the question. Sorry! So more simply: I want activate a Object with GUI button: 1. Firt the Object is deactive(disable or was...) 2. Player collusion a Trigger, Trigger activate a GUI button. 3. If I click GUI button, the Object is active. GUI button activate the Object.
Please help.
Your answer
Follow this Question
Related Questions
How To Deactivate A Parent (And Its Children)? 4 Answers
turn on a game object? 1 Answer
SetActive won't work 3 Answers
how to enable/disable slider VIA script from a prefab 0 Answers
Activate and deactivate some script when i press a Key. 2 Answers