- Home /
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 ,, :)
Answer by robertbu · Mar 27, 2013 at 04:42 PM
I'm assuming that ScriptA and ScriptC are on other game objects, so to start, you need to get access. Here is a link:
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 $$anonymous$$e,, :)
I Think how can i give the Fixed Target to $$anonymous$$y 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 $$anonymous$$uch
Your answer
![](https://koobas.hobune.stream/wayback/20220613095214im_/https://answers.unity.com/themes/thub/images/avi.jpg)