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

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 :stuck_out_tongue: ^^

Greetz, Ky.

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.