Public void has problem while activing from another class.
Greetings! I want to active public void EnemyDestroyed in another class, Enemy class; but once this function is activated, in the console Unity says: NullReferenceException: Object reference not set to an instance of an object Enemy.Update () (at Assets/Scripts/Enemy.cs:51). In the inspector of enemyPrefab, where the Enemy script is attached to, there is a blank instance called Spawn Enemy, and there is no such thing that can be filled in that blank. In the script, monodevelope does consider SpawnEnenmy in Enemy script as a public class, but in the inspector it doesn't. I guess I did something wrong while calling the public void, but just couldn't figure it out by myself.
public class SpawnEnemy : MonoBehaviour {
public Rigidbody enemy1;
public Rigidbody enemy2;
public Rigidbody enemy3;
public GameObject SpawnPoint;
public int enemyNum = 0;
public int difficulty = 0;
private float E = 0;
void Refill ()
{
E += 1.0f * Time.deltaTime;
}
public void EnemyDestroyed ()
{
enemyNum -= 1;
difficulty += 1;
}
// Update is called once per frame
void Update ()
{
if (enemyNum < 7 && difficulty <= 10 && E >= 1)
{
Rigidbody enemy1Instance;
enemy1Instance = Instantiate (enemy1, SpawnPoint.transform.position, SpawnPoint.transform.rotation) as Rigidbody;
enemy1Instance.AddForce (SpawnPoint.transform.forward * 1);
enemyNum +=1;
E -= 3.0f;
}
if (enemyNum <= 5 && difficulty <= 30 && E >= 1 && difficulty > 10)
{
Rigidbody enemy2Instance;
enemy2Instance = Instantiate (enemy2, SpawnPoint.transform.position, SpawnPoint.transform.rotation) as Rigidbody;
enemy2Instance.AddForce (SpawnPoint.transform.forward * 1);
enemyNum +=1;
E -= 3.0f;
}
if (enemyNum <= 4 && difficulty <= 60 && E >= 1 && difficulty > 30)
{
Rigidbody enemy3Instance;
enemy3Instance = Instantiate (enemy3, SpawnPoint.transform.position, SpawnPoint.transform.rotation) as Rigidbody;
enemy3Instance.AddForce (SpawnPoint.transform.forward * 1);
enemyNum +=1;
E -= 3.0f;
}
if (E <= 3)
{
Refill ();
}
}
}
public class Enemy : MonoBehaviour {
public SpawnEnemy Spawn;
public int life = 200;
public int moveSpeed = 1;
private bool move;
private int[] damage = {50, 25, 30};
void Start ()
{
move = true;
}
void OnTriggerEnter (Collider other)
{
if (other.gameObject.CompareTag ("CannonBall"))
{
life -= damage[0];
}
if (other.gameObject.CompareTag ("FireBall"))
{
life -= damage[1];
}
if (other.gameObject.CompareTag ("IceBall"))
{
life -= damage[2];
}
if (other.gameObject.CompareTag ("Gate"))
{
move = false;
}
}
// Update is called once per frame
void Update ()
{
if (move == true)
{
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}
if (life <= 0)
{
Spawn.EnemyDestroyed ();
}
}
void LateUpdate ()
{
if (life <= 0)
{
Destroy (gameObject);
}
}
}
to call it like that you need to make the method static, but a better way is to get a reference to the other script and call it that way.
look at GetComponent()
in the docs.
Answer by saschandroid · Jan 11, 2016 at 01:26 PM
Add a public variable for the game object that has the SpawnEnemy script on it to Enemy.cs
public GameObject m_spawnEnemyObject;
and drag the game object with the script onto it in the inspector. Then add in Start() of Enemy.cs
void Start()
{
Spawn = m_spawnEnemyObject.GetComponent<SpawnEnemy>();
}
Your answer
Follow this Question
Related Questions
How to call an int value from another void 1 Answer
Public float not in inspector 0 Answers
How to give Vector3 position to object being instantiated 1 Answer
Not finding a class in inspector (Easy) 0 Answers
In void bool dont changing 1 Answer