- Home /
Script for buttons referencing different Game Objects
Hi, I've got some box collider buttons on a panel as part of an augmented reality console. The buttons are triggered by rays. I need to write a script that references buttons 1 through 7, to activate/deactivate other game objects that are acting as video screens. This is the latest of my my poor attempts, and I'll be honest, every script I try is drag and dropped onto the camera, the buttons, the video planes, and a separate scripts game object. I appreciate this is simple stuff, which is all the more surprising I can't find an previous answer on here. Ta in advance. R
#pragma strict
private var ray: Ray;
private var hit : RaycastHit;
private var vid2 : GameObject;
private var vid5 : GameObject;
function Start () {
vid2 = GameObject.Find("Bottom right/Videos Panel/Vid2");
vid5 = GameObject.Find("Bottom right/Videos Panel/Vid5");
vid2.gameObject.SetActive(false);
vid5.gameObject.SetActive(false);
}
function Update () {
if(Input.GetMouseButtonDown(0)){
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, hit)){
if(hit.transform.name == "VidBt2"){
vid2.gameObject.SetActive(true);
vid5.gameObject.SetActive(false);
}
if(hit.transform.name == "VidBt5"){
vid2.gameObject.SetActive(false);
vid5.gameObject.SetActive(true);
}
}
}
}
$$anonymous$$ I'm getting closer but it's still not quite there, surely it's common to activate a separate hierarchy object / object's animation via a button and script it?
#pragma strict
private var ray: Ray;
private var hit : RaycastHit;
private var vid2 : GameObject;
private var vid5 : GameObject;
function Start () {
vid2 = GameObject.Find("Bottom right/Videos Panel/Vid2");
vid5 = GameObject.Find("Bottom right/Videos Panel/Vid5");
vid2.SetActive(false);
vid5.SetActive(false);
}
function Update () {
if(Input.Get$$anonymous$$ouseButtonDown(0)){
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, hit)){
if(hit.transform.name == "VidBt2"){
vid2.SetActive(true);
vid5.SetActive(false);
}
if(hit.transform.name == "VidBt5"){
vid2.SetActive(false);
vid5.SetActive(true);
}
}
}
}
And it's not working as it is? What is happening? Do you get errors?
I think I had the script in place multiple times and it was fighting against itself. It's on a separate scripts object now and works well. Now I have to do the same thing but access a script component in the hierarchy in order to turn THAT on and off. Should be easy in comparison.
Your answer

Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How to activate a script from another script at runtime 1 Answer
Object won't reactivate or activate with button press. 2 Answers
Changing gameobject assigned to a variable on input.getkey. 2 Answers
Combine objects instantiated at runtime 2 Answers