- Home /
How to use GetComponent to modify variables of multiple gameobjects with the same script
I have a gameobject with a script that will control the variables of other gameobjects, how do I modify the variables of the other gameobjects individually? All gameobjects I'm going to modify the variables with are the same script.
I use translator, sorry.
Answer by Zodiarc · Nov 17, 2017 at 03:44 PM
You either assign all the game objects to a list within the script which controls them or you assign a tag to those objects and use
FindGameObjectsWithTag()
in the control script
Helped me a lot, now I can identify each object with the tags. Thank you very much.
Answer by shadowpuppet · Nov 17, 2017 at 03:57 PM
in C# you need to make the variables you want to modify public static . Such as if i have a script called ammunition and another script on my gun so that when I shoot it I want to deplete the bullets in the ammunition script. I would have bullets be : public static int bullets;
//ammo script
using UnityEngine;
using System.Collections;
public class ammunition: MonoBehaviour {
public static int bullets = 10;
public int bulletsLeft;//I do this because as a public static you can't see it in the inspector.you don't need this and can delete when you see it's working.but for now I have in my update function bulletsLeft = bullets so I can see that they are indeed going down because the bulletsLeft will show in the inspector an be the same value as bullets
void Update(){
bulletsLeft = bullets;
}
}
//then in my gun script_____________________________________________________
using UnityEngine;
using System.Collections;
public class gun: MonoBehaviour {
MonoBehaviour bullets;// this is the static int variable I want to change
public GameObject ammo; //this is the gameobject with the ammunition script on it;
void Start(){
bullets = ammo.GetComponent<ammunitiion>(); //the variable I want to change = gameobject with script<the script itself>();
}
void Update(){
if (Input.GetKeyDown (KeyCode.Keypad0)) {
ammunition.bullets -=1;// so every time I press keypad 0 the bullet int on my ammunition script goes down by 1
}
}
in C# you need to make the variables you want to modify public static.
No you don't. Do you know what implies the static
keyword when declaring a class member? It means that the value of this class member will be shared by every instances of the class. Supposing you have two wepons of the class ammunition
, they will share the same ammount of bullets which does not make sense.
$$anonymous$$oreover, you use the static keyword, but you retrieve a class instance using GetComponent
. What is the point? If you retrieve the class instance, you don't need the static
keyword anymore.
oh, ok. I'm pretty new at this and I had asked a very similar question some time ago and that was the answer i got. or pretty similar anyway. This is not how I handle my shooting scripts it was just an example off the top of my head, but I don't get how if I had two ammo clips with the script "ammunition" on them that they share bullets. I mean if I shoot my gun and that gun is referencing a specific clip with the script ammunition on it how it would effect the script on another game object? I usean ammo$$anonymous$$anager script that keeps track of grenades, flares, pistol bullets, machine gun bullets and C4. Then a script on each that references the ammo$$anonymous$$anager and takes away the particular ammo I used when I used it. If i am holding a pistol i deplete the pistol bullets on fire button pressed, machne gun depletes machine gun bullets etc etc