# Is it possible to control what script is active on what sprite/object in Unity?

I have 2 objects, and I have a script which makes the player change their characteristics. I applied the script to both, and as you'd expect, pressing X button does the color, size, translation change effect on both objects at the same time.

Now how can I give the player the choice thanks to a key input, to be able to choose which item to apply the script to?

I am trying to make it work like a toggle..

if (toggle == 1) { // player can only change color of item 1 }

else if (toggle == 0) { //player can only change color of item 2 }


So what's the syntax to be able to select if a script is enabled or not on 2 different sprites?

It would be better to add a bool variable into your Script and then toggle it on/off.

var object1Script = object1.GetComponent<MyScript>();
var object2Script = object2.GetComponent<MyScript>();

object1Script.enabled = toggle;
object2Script.enabled = !toggle;

• Are you sure that is in C#? I am having errors trying to implement this, or maybe you could give me a little bit more details about object1, and where should I declare object1script? – Zee Mar 5 '15 at 14:06
• object1 and object2 are the two GameObjects which you said you're having. – Mihai-Andrei Dinculescu Mar 5 '15 at 16:23

something like this?

if (GetComponent<NameOfScript>().enabled == true)

• I actually want to enable it, or disable it, not check if it's enabled or not, sorry for the confusion – Zee Mar 5 '15 at 19:46