How to Set "Target" from two or more different script?

Hi, i just have a problem with my script, I have a problem when i Set The “Target” from another script. Maybe something like this:

from script A -Give a Target> Script B → Instantiate

from script C -Give a Target> Script B → Instantiate

Its work when the Script A just Instantiate the Script B, Then When Script C give an order to Script B, The Target from Script A is Changed to Target from Script C ,

How can i get the “Fix” target for my Script when i Instantiate the Script?

Sorry for my English , :slight_smile:

1 Answer

1

I’m assuming that ScriptA and ScriptC are on other game objects, so to start, you need to get access. Here is a link:

Accessing Other Game Objects

After than it is easy. If ‘Target’ is a global variable of ScriptB, then with a reference to ScriptB, you set it directly. Or you could add a method to ScriptB that takes ‘Target’ as a parameter. If you post some of your code, we could give more precise advice.

I use GetComponent for scipt B, but still same, i use this script for Script A and C. But With The Different Target and Variable var Target : Transform; var Bullet : GameObject; var shoot : boolean = false; private var myGO : GameObject; function Update() { if(shoot) { ShootTheBullet(); } } function ShootTheBullet() { myGO = Instantiate(Bullet,transform.position,transform.rotation); var theScript = myGO.GetComponent(ScriptB); theScript.Target = Target; Shoot = false; }

So this script looks fine. Is there still a problem, or is this the solution?

the problem is when i Instantiate the Bullet,,, when i "Shoot", The Bullet is go to the my target, but when my bullet still go to my target, then the AI "shoot" the bullet with the target is me,my Bullet Turn Around and Attack Me,, :) I Think how can i give the Fixed Target to My Bullet?

Can you post the script for the bullet (ScriptB). Did you make the Target you set for the bullet static?

yeah,,,Problem Solved I Set a Variable in scriptB as a Static var,, when i Change it to Public,, Its Work Thanks Very Much