- Home /
GetComponent giving error
I am starting to make a script that holds all the changeable values and use GetComponent to access them. I have looked at allot of other tutorials, even the unity one but I still cant figure it out. I am getting the error:
NullReferenceException: Object reference not set to an instance of an object
Enemy.FixedUpdate () (at Assets/_Scripts/Enemy.cs:29)
Here is my value storage script:
using UnityEngine;
using System.Collections;
public class gameStats : MonoBehaviour {
public int orcHealth;
public int weaponShortSwordMinDamage;
public int weaponShortSwordMaxDamage;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
And my Orc script:
using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour {
Animator anim;
//public float health = 100;
public bool canDamage = true;
public float damageTimer;
public float damageTimerOriginal;
private gameStats Stats;
// Use this for initialization
void Awake ()
{
Stats = GetComponent<gameStats>();
}
void Start () {
anim = GetComponent<Animator>();
}
//Used to see if the Orc is dead
void FixedUpdate ()
{
if(Stats.orcHealth < 1)
{
anim.SetTrigger("Dead");
}
if(canDamage == false)
{
damageTimer = damageTimer - Time.deltaTime;
}
if(damageTimer < 0)
{
canDamage = true;
damageTimer = damageTimerOriginal;
}
}
// Update is called once per frame
void OnTriggerEnter2D(Collider2D other) {
if (other.tag == "WSS" && canDamage == true)
{
Stats.orcHealth = Stats.orcHealth - 10;
//Leaveing this out for now:
//Random.Range(WSSMinDamage, WSSMaxDamage);
Debug.Log(Stats.orcHealth);
canDamage = false;
}
}
}
BTW I am working in 2D
Here is a view of my game so far, maybe you can spot a problem.
Thanks for the help :)
Answer by ejpaari · Jun 16, 2014 at 06:34 AM
You are trying to get the Game Stats -component of the Enemy-object even though the component is in the Stats-object.
Following code tries to access the component of the same object (in this case, Enemy-object's Stats-component)
Stats = GetComponent();
but you could use instead, for example
Stats = GameObject.Find("Stats").GetComponent();
or some other method. On the other hand you could attach the Stats-object to your Enemy-object depending how you are designing your game.
Thanks guys for the help but now im getting the error:
Assets/_Scripts/Enemy.cs(18,50): error CS0411: The type arguments for method `UnityEngine.GameObject.GetComponent<T>()' cannot be inferred from the usage. Try specifying the type arguments explicitly
$$anonymous$$y enemy script looks like this now:
using UnityEngine;
using System.Collections;
public class Enemy : $$anonymous$$onoBehaviour {
Animator anim;
//public float health = 100;
public bool canDamage = true;
public float damageTimer;
public float damageTimerOriginal;
private gameStats Stats;
// Use this for initialization
void Awake ()
{
Stats = GameObject.Find("Stats").GetComponent();
}
void Start () {
anim = GetComponent<Animator>();
}
//Used to see if the Orc is dead
void FixedUpdate ()
{
if(Stats.orcHealth < 1)
{
anim.SetTrigger("Dead");
}
if(canDamage == false)
{
damageTimer = damageTimer - Time.deltaTime;
}
if(damageTimer < 0)
{
canDamage = true;
damageTimer = damageTimerOriginal;
}
}
// Update is called once per frame
void OnTriggerEnter2D(Collider2D other) {
if (other.tag == "WSS" && canDamage == true)
{
Stats.orcHealth = Stats.orcHealth - 10;
//Leaveing this out for now:
//Random.Range(WSS$$anonymous$$inDamage, WSS$$anonymous$$axDamage);
Debug.Log(Stats.orcHealth);
canDamage = false;
}
}
}
Answer by phxvyper · Jun 16, 2014 at 06:38 AM
Ejpaari is correct. However, for efficiency's sake, attach the GameStats component to the player, as there is no real reason for it to be attached to a separate empty object!
Once you do this, you can use GetComponent();
or GetComponent(typeof(gameStats));
to find that instance of the gameStats component on your player!
Answer by hayer · Jun 17, 2014 at 09:33 AM
Since you are using C# I would suggest using the more "C#ish" version; GetComponent where the is a generic type which inherits from Component. So if you want to get a SpriteRenderer you would write;
SpriteRenderer srInstance = gameObject.GetComponent<SpriteRenderer>();
To get the SpriteRenderer of the gameObject the script is attached too. This can also be used like this;
SpriteRenderer srInstance = GameObject.Find("MyGameObject").GetComponent<SpriteRenderer>();
Seems like the formatting for the 2nd code snippet is broken.
Your answer
![](https://koobas.hobune.stream/wayback/20220613151645im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
how to use bool from another script in if statment? 2 Answers
Is there a way to find other's components using OnTriggerEnter? 1 Answer
if it's you, how will you solve it ? 1 Answer