how to controll a variable of a script from another script
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 "
Answer by ArturoSR · Aug 21, 2016 at 10:20 PM
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, i made this code but my int zombie amount does not change
using UnityEngine;
using System.Collections;
public class EnemyBehaviour : $$anonymous$$onoBehaviour
{
SpawnScript componentZombieSpawn = null;
public int life = 100;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (componentZombieSpawn != null)
{
componentZombieSpawn.zombie_amount -= 1;
}
if (life <= 0)
{
Destroy(this.gameObject);
}
}
void OnTriggerEnter(Collider Bullet)
{
life -= 10;
print("am fost lovit " + Bullet.name);
}
}
and the one with the variable
using UnityEngine;
using System.Collections;
public class SpawnScript : $$anonymous$$onoBehaviour {
public int zombie_amount = 4;
public GameObject zombie;
// Use this for initialization
void Start () {
Instantiate(zombie, transform.position, transform.rotation);
}
// Update is called once per frame
void Update () {
if (zombie_amount <= 0)
{
Instantiate(zombie, transform.position, transform.rotation);
zombie_amount = 4;
}
}
}
What do you get using this?, perhaps you forgot to add the Instantiated Zombie to the SpawnerScript variable.
no error so far, but the zombie_amount remains the same
Answer by SQUARE-BADGER · Aug 25, 2016 at 11:06 PM
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);
}
}
Have you Skype or Facebook?, I like to make a more direct contact with you, this way I can help you to resolve you problem more quickly, what do you think?.
good idea, Andrei Nica on facebook and nica.andy1 on skype
Your answer
Follow this Question
Related Questions
Variable from another script isn´t updating 3 Answers
Assigning variables depending on other variables 1 Answer
Problem with the Inspector and Script 1 Answer
Help with variables 1 Answer
Use enum hidden value as something other than int? (C#) 2 Answers