How can I find a script component attached to a gameobject when there are multiple copies of that script attached?

I’m making a rpg and am working on a fireball projectile. this projectile gameobject is instantiated by my player via a special attack script, of which there are 3 attached to my player. Each of these special attack scripts have different values, and I wish to only access the values of the fireball special attack. They are called using different assigned keys.
The fireball, once instantiated, is controlled by a strike script which passes the damage and effects onto the enemy upon “OnTriggerEnter”.

The problem I have is in accessing the damage and stun variables of the players fireball special attack from the projectiles strike script. Since my player has 3 special attack scripts attached, how would I go about getting the correct script’s variables?

I’m happy to post any scripts if necessary, please ask. Thanks!

What I would do is pass the reference to the projectile on instantiation. Let the player get the strike script from the instantiated projectile and call SetValues() method or something, which has the specialattackscript as a parameter. Save it in the strike script. OnTriggerEnter the projectile will then know where to look.

Firstly, why are there 3 versions of the same script? I imagine you have variables you set to each of the versions of the script, but could you not have 3 different special attack scripts (one for each type of special) or maybe put all 3 into one, expanding the number of variables to allow for the different scripts? There is likely a reason you have chosen this approach, but not one I can think of :slight_smile:

But: I would pass the variables to the strike script on instantiate (similar to hexagonius suggested). Have Damage and Effect as public variables on the strike script and then assign them when you instantiate the projectile:

GameObject obj = Instantiate(projectilePrefab, //etc) as GameObject;
obj.GetComponent<Strike>().damage = damage;
obj.GetComponent<Strike>().effect = effect;