i am kinda dum at this and i need some help, i have a script and i wanted to change a int variable from another script
i have a script when a specific object is destroyed and i want to ad a "change the variable of the x script to -1when destroyed or something
"
Hi, ok, this is what you need to do, the easy way it is to get access to the script component from the other object, something like this:
//Object 1 - Component "Shield"
public int damage = 0;
void Update {
if (damage >= 20) {
Destroy(this.gameObject);
}
}
//Object 2 - Component "Weapon"
Shield componentObject1 = null;
void Update() {
if (componentObject1 != null) {
componentObject1.damage += Random.Range (1, 5);
}
}
void OnTriggerEnter(Collider collider) {
//Here you can get the object that enter on the trigger and get its component.
componentObject1 = collider.gameObject.GetComponent<Shield>();
}
//Remember, this is just an example, not already your code.
Only you have to do it is to drag and drop the “other” object with the Shield component attached to it, you will notice that the variable type from the Object 2 has the same “name” as the script component from the Object 1, this is known as Class and you can reference any public class, you can gain a direct access to any field, property, method or function with this.
Using this method you can modify any variable from any script component, remember to make any target variable or function public to gain access to it, cheers.
ok so i put a debug on the void update
and it still does not react, the zombie spawn object does not change its zombie_amount int, and the debug does not show anything i tried to solve this but i am stuck with this 3 days in a row
void Update()
{
if (componentZombieSpawn != null)
{
componentZombieSpawn.zombie_amount -= 1;
}
if (life <= 0)
{
Destroy(this.gameObject);
}
}