how to get a component from another script and affect the changes on it as well
hello, im trying to make two separate scripts for my platformer project in school; they are for the spawning of enemies and the other is for the counter to how many enemies they could respawn before the boss arrives. here are my scripts
enemyrespawncontroller:
public Transform OnePrefab;
public Transform TwoPrefab;
public Transform ThreePrefab;
public Transform FourPrefab;
public int x;
public float upperlimit = 4.0f;
public float lowerlimit = -1.0f;
public float everyseconds = 2.0f;
public float respawnlimit = 10.0f;
public float delay = 0;
public enemycountercontroller enemycoun = null;
void Start () {
enemycoun = GetComponent<enemycountercontroller> ();
}
void FixedUpdate () {
if (Time.time % everyseconds == 0) {
Invoke ("GenerateEnemy", delay);
enemycoun.z = enemycoun.z - 1;
}
}
void GenerateEnemy(){
x = Random.Range (0, 4);
if (x == 0) {
Instantiate (OnePrefab, new Vector2 ( respawnlimit, Random.Range (lowerlimit, upperlimit)), OnePrefab.rotation);
} else if (x == 1) {
Instantiate (TwoPrefab, new Vector2 ( respawnlimit, Random.Range (lowerlimit, upperlimit)), TwoPrefab.rotation);
} else if (x == 2) {
Instantiate (ThreePrefab, new Vector2 ( respawnlimit, Random.Range (lowerlimit, upperlimit)), ThreePrefab.rotation);
} else if (x == 3) {
Instantiate (FourPrefab, new Vector2 ( respawnlimit, Random.Range (lowerlimit, upperlimit)), FourPrefab.rotation);
}
}
}
(Note that i have a clone script called groundenemyrespawncontroller, which functions the same as enemyrespawncontroller)
and here is the enemycountercontroller:
public int z = 100;
void Start () {
}
void Update () {
if (z <= 0) {
}
}
}
-assume if z reaches 0, then boss appears
i tried those, but the counter didnt move down. and there is an error every time it spawns:
NullReferenceException: Object reference not set to an instance of an object groundenemyrespawncontroller.FixedUpdate () (at Assets/scripts/groundenemyrespawncontroller.cs:24)
how do i fix this problem please answer asap thanks
Groundenemyrespawncontroller is much the same as enemyrespawncontroller
Your error is in groundenemyrespawncontroller. If you don't show us the script your error is in. we can't help you. You should also post your code starting from the very top of the script so we can see which line the error is in.
What i mean is groundenemyrespawncontroller is exactly a clone of the enemyrespawncontroller. It just so happens the error from groundenemy went first. But they are both exactly the same.
Also when you mean "post your code starting from the very top of the script", did you mean including the libraries used?
The problem you have is probably how the GameObject and components are put together. Is all the scripts on the same GameObject? GetComponent<> only looks at the GameObject this script is on.
Your answer
Follow this Question
Related Questions
If several BoxCollider attach to a gameObject, how to differ them? 0 Answers
Trigger enabling component works in one statement but not another? 1 Answer
No overload for method. 1 Answer
Why won't script work on 3d text? (menu script) 1 Answer
Scrpits component disappears when I enter playmode 0 Answers