- Home /
"Object Reference not set to an instance of an object" What does it mean?
Alright, so I have this bit of code set up, which helps my character attack things. When I press the "A" key, and left click on the screen, I set my character to attack that point on the screen. Everything goes well up until he gets within attack range. The attack instance, attackPrefab, does not actually appear where it should. Here is my code:
using UnityEngine;
using System.Collections;
public class gladiator_combat_script : MonoBehaviour
{
public GameObject character;
private gladiator_base_stats stats;
bool attackingClick = false;
public bool attacking = false;
float targetDistance;
Vector3 attackTrue;
public GameObject attackPrefab;
public GameObject movePoint;
void Update()
{
if(Input.GetKey("a"))
{
attackingClick = true;
}
if (Input.GetMouseButtonDown (0))
if(attackingClick == true)
{
RaycastHit attackPoint;
Vector3 attackPos = Input.mousePosition;
attackPos.z = 25f;
Debug.Log(Camera.main.ScreenToWorldPoint (attackPos));
attackTrue = Camera.main.ScreenToWorldPoint (attackPos);
Physics.Raycast (Camera.main.transform.position,attackTrue, out attackPoint);
Debug.Log (attackPoint);
movePoint.transform.position = new Vector3(attackTrue.x, -0.45f, attackTrue.z);
attacking = true;
}
if (attacking == true)
{
targetDistance = Vector3.Distance(transform.position,attackTrue);
Debug.Log (targetDistance);
if (targetDistance < stats.attackRange)
{
if (stats.attackCooldown == 0f)
{
Rigidbody attackInstance;
stats.attackCooldown = Time.deltaTime * 1;
attackInstance = Instantiate(attackPrefab, attackTrue, character.transform.rotation) as Rigidbody;
}
}
}
}
Which means this line: "if (targetDistance < stats.attackRange)" should be the source of error. Does anyone know how to make the attackPrefab actually GET created? Thanks!
Check if you have assigned attackPrefab in the inspector before running.
I don't see you initialize "stats" anywhere in the code. If stats is null, you will get exception on stats.attackRange.
Yes, you may want to initialize it in Start() or Awake() using GetComponent() function.
Answer by trex1121@aol.com · Apr 24, 2014 at 09:25 AM
Same info as everyone else. You are using stats (which is a reference/instance of gladiator_base_stats) but you have not fully declared it as an instance before using it. You are currently using it as if it is a value type.,
Answer by Ianson · Apr 24, 2014 at 10:54 AM
As other have said you don't actually instantiate (read "create") stats so when you try and reference it it isn't there.
Depending on where the stats are supposed to come from you have a few options:
A: If you only want to check attack range after some other part of your code has instantiated and initialised gladiator_combat_script.stats then wrap the offending code in a null check:
if( this.stats != null )
{
/// do stuff
}
This could include making it public and assigning it through the editor, IF it is a monobehaviour...
B: as others have say you simply need to instantiate stats: // Either do it when you declare it private gladiator_base_stats stats = new gladiator_base_stats();
// Or, as other have said do it in start
void Start()
{
stats = new gladiator_base_stats();
// do any initialisations
}
Your answer
