- Home /
Problems with buttons all turning on or off at the same time
Hello i have a problem with my button prefab.
Im having diferent buttons all over my game, and its importent that i know if they are on or off. The button script where its turned on or off is in one script (Script A) where the activation of that scripts is in my player script (Script B).
So I need Script B to trigger a function on Script B. Pretty simple i got that to work, but its doing it on all the prefabs when im doing it this way.
Can someone write some code or explane how im changing my Boolean on onely one object insted of all the prefabs.
The button comes from a prefab with a script.
Thanks for your time reading this question.
Edit: Sorry for that, this is my fist question and not sure how to set it up i will give some code here:
Script B
var UseScript : GameObject;
//*Public Variables End*
function Update ()
{
if((Input.GetKeyDown(controllsUse) && inUseRange) || (Input.GetKeyDown(controllsUse2) && inUseRange))
{
Use();
}
}
function Use()
{
print("use");
UseScript.GetComponent(JsObjectsButtons).UseButton();
}
function OnTriggerEnter (other : Collider)
{
if (other.tag == "Button")
{
print("In");
inUseRange = true;
}
}
function OnTriggerExit (other : Collider)
{
if (other.tag == "Button")
{
print("Out");
inUseRange = false;
}
}
**Script A**
//useable Objects
//Public Variables
var objKind : int;
//Private Variables
var isOn : boolean = false;
function Update ()
{
UseButton();
}
public function UseButton()
{
if(isOn)
{
isOn = false;
renderer.material.color = Color.blue;
}
else if(!isOn)
{
isOn = true;
renderer.material.color = Color.green;
}
print(isOn);
}
Answer by Slasmir · Feb 09, 2012 at 08:23 AM
I fixed it, as soon as i posted the code i saw the problem. i where editing the overall prefab not the single i have now added that the script changes ontrigger enter.
Enyways thanks alot for helping me how to post a question Aldonaletto, and for being nice to my bad post :)
Answer by aldonaletto · Feb 08, 2012 at 10:48 PM
You should post your code - how could we know what's wrong without seeing it?
Anyway, using the telepathic powers I developed in Unity Answers, I got a feeling that your problem may be caused by some static boolean variable.
A static variable is created only once, and is the same for all instances of the script - thus different prefabs will see the same value.
If my feeling is right, you still must edit your question and post the scripts A and B, because the solution will depend on how they are written - you may have to avoid static vars, or use some other trick to affect only the desired object.
Your answer
![](https://koobas.hobune.stream/wayback/20220613063906im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Zombie Round Script Help 1 Answer
Spawn Script Not Working 1 Answer
Adding commands to buttons? 1 Answer
[SOLVED] Problem with "foreach". 1 Answer
Making A Script for my Switch 1 Answer