- Home /
Talking To Other Scripts
Hello,
I don't know if I'm misunderstanding the key elements here for a script to talk to another script, but it always seems to be an on, off thing (Null Refs).
Okay,
I basically need one script to talk to another. These scripts are in the same parent. Here is my layout:
Parent > Parent > Child1 | Child2 | Child3
All I want is for Child 3 to talk to Child 1
Child 3 detects a trigger collision, and needs to set a boolean in Child 1 to true if a collision occurs.
Its that simple, and I use this methord so many times before, and sometimes it works, sometimes it doesn't:
var other : ScriptName;
other = gameObject.GetComponent("ScriptName");
other.boolean = true;
Could someone please explain to me why this particular communication isn't working?
Just out of interest, is there a way to do this? :
var TriggerDetection : GameObject;
function Update(){
if(TriggerDetection.ScriptName.boolean == true){
Answer by FTheCloud · Aug 04, 2011 at 05:05 PM
Use BroadcastMessage. It allows you to literally tells the other script that its colliding and what to do when it's colliding.
Try this for child3:
var Child1 : GameObject;
var Collide : boolean = true;
function OnCollisionEnter (collision : Collision) {
Child1.BroadcastMessage("Message", Collide, SendMessageOptions.DontRequireReceiver);
}
And this for Child1:
function Message (Collide : boolean) {
if (Collide){
//Whatever...
}
}
These are just simple examples of what you can do. Go crazy
Sweeeeet! It works -- Could I have the child1 : GameObject, to be an array? So it sends the message to multiple objects? Because when I try that, I get a null.