- Home /
One GameObjects Script Activating Another GameObjects Script
Hello I'm very new to Unity and I will say it is an amazing engine. But for some time now, I've had this dilemma of witch I have no idea how to fix or do, and that's activating and GameObject to effect another. I have looked around and used codes like...
(For Java Scripts)
otherObject.GetComponent(NameOfScript).enabled = true;
But seem to get no were, if it is possible, could someone show me the right way to do these sorts of codes in a little script example.
My situation is this, this is on a main menu and there's is a button and a camera. Lets say when I click the button (named "NewGame"), it goes to a new scene, but before that, I want a little animation of the camera(Named "Maincamera") moving in. There's already a script attached to the camera(script named "cameramove") that when activated, plays the animation. How would the script for the button look like if it activates the camera script.
Cheers
Answer by Anxo · May 29, 2011 at 08:22 PM
well you first want to put everything in a varible, the way your going about it, may work like this
var objectB : GameObject;
var objectBScript;
function Awake(){
objectB = GameObject.FindWithTag("objectB");
objectBScript = objectB.GetComponent(superscript);
}
function TurnOnScript(){
objectBScript.enabled = true;
}
Another way you could handle it would be to just use a boolean as a switch in object B so
on Object be
var Off : boolean = true;
function Update(){
if (Off)
return;
//this line would not run till off is turn off.
}
function TurnOffOff(){
Off = false;
}
then in object A you would have a script like
var objectB : GameObject;
function Awake()
{
objectB = GameObject.FindWithTag("objectB");
}
function turnOffOFf(){
objectB.BroadcastMessage("TurnOffOff");
//sends a message to object B to run the turnoffoff function
}
Thank you for the quick response, but still it does not want to work. I've done exactly what you said and copied them into my scripts, I renamed them to there proper names, set everything in the right order and tested, no luck. I even made a new scene and named everything like you did, but still, it was a no go. This is a really need thing for the type of game I'm making. I just have no other idea how I will do this.
Again though, thank you.
are you sure your tagging the object and not just creating the tag? I use gameObject.Broadcast$$anonymous$$essage("function"); all the time.
oh you also have to call turnOffOff, so like in Awake or how ever you want to trigger it, you need to call the function like
function Start(){ turnOffOff(); }
I think I'm starting to understand this more, the only one thing I'm a little curious about is the tags, you said "are you sure your tagging the object and not just creating the tag?", so would that mean i can only choose one of the 7 tags available?