- Home /
Accesing another Script on gameObject and asking if it is true.
Alright so Im a bit stuck. What I am trying to do is access a script on the gameObject which is where I handle damaged (I prefer to keep a few things out from my AI script such as damage) and what I am trying to do it that when I shoot my enemy when I am out of his field of view he changes his state to attacking me.
Now this is simple enough you can just put an if statement wondering if that scripts static boolean values gettingHurt is true. This works fine but if I shoot an enemy with the damage receiving script since that scripts become true the AI script reads through all damage receiving scripts and since one gettingHurt is true it changes its state.
if (DamageReceiver.gettingHurt == true){
transform.LookAt(target);
if (ableToShoot == true)
{
InvokeRepeating("Shoot", 0.001f, gunROF);
ableToShoot = false;
}
}
(This is a seperate AI code for a MG nest which is not using a state machine but it still needs the same thing done to it. Figured I might show some script.)
Also I have tried using gameObject.GetComponent in various methods but I can not seem to get it to work right or I get the error (something forgot the first part as I have reverted script so it works so I could work and test other things )cannot be accessed with an instance reference, qualify it with a type name instead
Thank you for your help :)
Answer by Lo0NuhtiK · Jan 16, 2012 at 07:01 AM
You said above that your gettingHurt variable is a static variable, so (from what I've gathered) changing that variables value anywhere will change it's value everywhere .
If you want each AI to have it's own instance of that variable, then it needs to be non-static.
quick example of how to access the AI script from a projectile->
var health_AI : AI_HealthScriptName ;
function OnCollisionEnter(other : Collision){
//get the script from the AI this projectile just hit
health_AI = other.collider.gameObject.GetComponent(AI_HealthScriptName) as AI_HealthScriptName ;
//set the hurtvariable
health_AI.gettingHurt = true ;
//extra example assuming we're going to take some health away somehow
health_AI.currentHealth -= 50 ;
//reset the health_AI to nothing/null since we're done with it
health_AI = null ;
}
Something along those lines.
/facepalm. No wonder. Ill have a play and see if I can work it out.
Success I managed to erase that static part in my script and it worked straight. It is always them easy things that get me :/
Cheers $$anonymous$$ate,
Your answer
Follow this Question
Related Questions
Unity crashes after I destroy a gameobject 1 Answer
How to recycle random prefabs that spawn from a specific point. 1 Answer
Cannot Locate Boolean in different script 4 Answers
Collecting Array items in order 1 Answer
check if object is moving 5 Answers