- Home /
How to send vars!
all i want to do is send my attackTarget and targetAquired vars to my other script so i can use them, but every time i try to send it it gives me a null.... its so frustrating does anybody know how i can send these two from one script to the other... here is my set up:
attackRTriggereStay.JS
@HideInInspector
var targetAquired = false;
var attackTarget;
function OnTriggerEnter (other : Collider) {
print(other);
print(targetAquired);
if(other.gameObject.CompareTag ("enemy")){
attackTarget = other;
targetAquired = true;
print(attackTarget);
print("targetAquired");
}
}
function OnTriggerExit (other : Collider) {
if(other.gameObject.CompareTag ("enemy")){
targetAquired = false;
print(attackTarget);
print(targetAquired);
}
}
LookAt.JS
var getTrigger : attackRTriggerStay;
var getTarget : attackRTriggerStay;
function Start(){
getTrigger = GetComponent(attackRTriggerStay);
}
function Update () {
getTarget =gameObject.GetComponent(attackRTriggerStay);
if(getTrigger.targetAquired == true){
transform.LookAt(getTarget.other);
}
}
Answer by FTheCloud · Aug 15, 2011 at 07:17 AM
Why don't you use BroadCastMessage to talk to the other script instead?
how would i incorporate it though? dont you have to use a function?
Broadcast$$anonymous$$essage is somewhat expensive, which can be okay if it isn't used very often, but usage should be kept to its actual purpose, which is to call a method on every $$anonymous$$onoBehavior attached to this GameObject or any of its children, i.e. it's useful when you have a non-static amount of children because child-objects get added at runtime.
If all the OP wants to do is pass variables from one script to the next, the easiest way is to simply expose the required variables through properties, and then save a reference to one of the scripts inside the other. That reference can then be used to access the properties with the required variables.
Google for Unity, JS, Property, access scripts and GetComponent
Your answer
Follow this Question
Related Questions
Respawn looking 1 Answer
Aim flashlight at cursor in third person 1 Answer
how do i make somthing rotate with mouse 1 Answer
lookat() issue 1 Answer