- Home /
LookAt Trigger for multiple objects
the below script is my script that i use to detect if the gameobject has a tag of enemy: SCRIPTS NAME IS: attackRTriggerStay.js
@HideInInspector
var targetAquired = false;
function OnTriggerEnter (other : Collider) {
print(targetAquired);
if(other.gameObject.CompareTag ("enemy")){
targetAquired = true;
print("targetAquired");
}
else {
targetAquired = false;
print(targetAquired);
}
}
i want the below script to edit a different object from the attack radius.
SCRIPTS NAME: LookAt.js
function Update () {
if( attackRTriggerStay.targetAquired == true)
transform.LookAt(attackRTriggerStay.other.transform.position);
}
my code on top is working perfectly but then when i try to compile with the second code its giving me this error AN INSTANCE OF TYPE'attackRTriggerStay' is required to acces non static memeber 'targetAquired'
so all that i am looking for is for my bottom script to recognize the value of targetAquired from the top script
Do you mean to use
function OnTriggerStay(other.collider){ if(other.name == "target"){
transform.LookAt(other.transform.position); }}
I am just guessing here, but I'm not sure of your goal. I Need more code to help further
i edited it to make more sense sorry for the confusion
That means 'attackRTriggerStay' is undefined. The editor doesn't know what it is or what to do with it- is it a game object? a tag? a name?
Have you tried adding my code to your LookAt.js above the function Update? (of course you have to put the name of an acctual Object ins$$anonymous$$d of THeObjectThatattackRTriggerStayIsAttatchedTo....)
As a sidenote: you should start Scriptnames and Functionnames with uppercase. Variables with lowercase. (People will be confused if you don't stick to that convention)
Greetz, $$anonymous$$y.
Answer by SisterKy · Aug 04, 2011 at 11:49 PM
Currently, since you don't tell Unity where it should look for 'attackRTriggerStay' it just takes 'the Script' instead of 'this Script over here'
You need to tell it where to find the Script.
So you have several towers.
each tower has its own LookAt.js
each tower has its own child-Cylinder with a attackRTriggerStay.js ?
no tower has more than one child with attackRTriggerStay.js ?
Ok then...
Edit: I realized another problem. You won't be able to access 'other' in this line:
transform.LookAt(attackRTriggerStay.other.transform.position);
Add this to attackRTriggerStay.js:
var targetAquired = false; var theTarget : Transform;
function OnTriggerEnter (other : Collider) {
theTarget = other.transform;
So now in LookAt.js you can do:
var myAttackRadius : attackRTriggerStay;
function Start ()
{
myAttackRadius = GetComponentInChildren(attackRTriggerStay);
}
function Update ()
{
if(myAttackRadius.targetAquired == true)
transform.LookAt(theTarget.position);
}
I think this should do...
Note on 'other':
The reason we have to assign 'other.transform' to the 'theTarget'-variable is, that 'other' only exists inside the OnCollisionEnter-Function. You can't access it from the outside.
Note on 'Start':
The thing I do in Start () means I look for the PATH to your other SCRIPT. While the variable 'targetAquired' will constantly change, and while the actual target will constantly change, the path to where this variable can be found will always be the same.
Extended Note on 'Start':
there are 2 general types Variables
value type: holds a number or word etc.
boolean
int
float
string
reference type: holds the path to the Instance of anything derived from UnityEngine.Object
(Instance means a copy of a script when it's attached to a GameObject)
Transform
GameObject
Collider
basically anything listed under ('derived from') 'Object' in the list of RuntimeClasses (jep, really all of those!)
MyOwnClass(=MyOwnScriptname)
note: you can not reference a value type
e.g. you have GameObjectA with a ScriptA with a ValueA=12 and want to access it from GameObjectB
VariableB = PathToGameObjectA.PathToScriptA.ValueA;
// this assigns the *content* of ValueA (=12)
VariableB += 5;
// this adds 5 to 12 = 17 and assigns it to VariableB; ValueA does not change.
vice versa, if ValueA is changed by ScriptA, VariableB stays 17 unless you repeat the above assignment
VariableB = PathToGameObjectA.PathToScriptA;
// this assigns the *Path* to ScriptA
VariableB.ValueA += 5;
// this adds 5 to 12 = 17 and assigns it to ValueA
If ValueA is changed by ScriptA, VariableB.ValueA knows about it.
So the second part is what I used in Start()
the variable 'myAttackRadius' references 'this attackRTriggerStayScript over here'.
then in Update we always look at the addressed Script if its Variables got updated and take appropriate actions to kill that evil intruder :p ^^
Greetz, Ky.
@Sister$$anonymous$$y sweet i am very thankful for the help, but your going to have to explain to me what the second LookAt.js script is doing in more depth as i am not that advance yet. the update makes sense but the function start and my attack radius are confusing me.
the first part was genius though i would have never thought to make other be defined by another variable so i could define it easier!
Here, take a look at this: http://unityscript.com/ =) Some parts of this may be a bit too basic for you but I think you can still benefit.
Greetz, $$anonymous$$y.
that was awesome! the reference was kinda way to basic ha ha but your explaination was spot on
I know how you feel... Reference is not too basic though... it's too advanced... it expects you to already know all this stuff. Oh, I know so well how you feel...! nods knowingly Glad I could help =)
Answer by GuyTidhar · Aug 04, 2011 at 08:16 PM
attackRTriggerStay was not instantiated (if it is a monobehaviour class) or constructed (if it a non mono behaviour class). "Instance" is a result of creating an object in memory - holding all relevant data in the code.