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.
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
^^
Greetz, Ky.