Reputation: 5
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(flipPlayer))]
public class enemyInstantiate : MonoBehaviour
{
public GameObject[] enemies;
public Transform enemyPos;
public GameObject enemyClone;
void Start()
{
enemyPos = GameObject.Find("enemySpawn").transform;
enemyClone = GameObject.FindGameObjectWithTag("Player");
enemySpawn();
flip();
}
public void enemySpawn()
{
int enemyIndex = Random.Range(0, enemies.Length);
Instantiate(enemies[enemyIndex], transform.position, transform.rotation);
}
void flip()
{
enemyClone.GetComponent<flipPlayer>().enabled = true;
}
}
NullReferenceException: Object reference not set to an instance of an object enemyInstantiate.flip () (at Assets/Scripts/enemyInstantiate.cs:32) enemyInstantiate.Start () (at Assets/Scripts/enemyInstantiate.cs:18)
i'm pretty new to Unity 3D And still having trouble, can you please help out with what the problem is and why am i getting a nullReferenceException.
The error occurs in the line (enemyClone.GetComponent().enabled = true;).
Upvotes: 0
Views: 13136
Reputation: 581
Probably in this line
enemyClone = GameObject.FindGameObjectWithTag("Player");
Is returning null
to var enemyClone
, and/or in GetComponent<flipPlayer>()
from line
enemyClone.GetComponent<flipPlayer>()
also is returning null
.
When you try to access a member of an object that is the null reference, this error happens.
Therefore, a way to check which reference is null, is debugging via MonoDevelop.
Upvotes: 1
Reputation: 998
Without the complete error, the only things I can say are:
Hope this helps you! If not, please post the line in which the null reference is happening
Upvotes: 1