- Home /
Object reference not set to an instance of an object error.
Baisicly I have deleted 99% of unnececary code, but this error "Object reference not set to an instance of an object error". is occouring in Test script, You can find with "ERROR SHOWS HERE" I have spend multiple hours to find similar cases in unity answers, I dont understand why it does not work. Why I cant launch StartBuff from another script via find object, get the script and launch StartBuff with these values...
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
public class StatusEffect : MonoBehaviour
{
public int MovePowerModif;
public int TurnPowerModif;
public int StrModif;
public int AgiModif;
public int IntModif;
public int FortModif;
public int WillModif;
public List<Buff> BuffList;
// Test if list works.
public void BuffManager()
{
if (BuffList.Count != 0)
{
foreach (Buff buff in BuffList)
{
Debug.Log (buff.EffectName); // test
}
}
else
{
Debug.Log("Buff list empty.");
}
}
public void StartBuff(string NewEffectName, int NewEffectPower, string NewEffectStat, int NewEffectDurration, string NewEffectDescription)
{
string EffectName = NewEffectName;
int EffectPower = NewEffectPower;
string EffectStat = NewEffectStat;
int EffectDurration = NewEffectDurration;
string EffectDescription = NewEffectDescription;
BuffList.Add (new Buff (EffectName, EffectPower, EffectStat, EffectDurration, EffectDescription));
BuffManager();
StartCoroutine(BeginCountdown(EffectDurration));
}
IEnumerator BeginCountdown (int Durration)
{
yield return new WaitForSeconds (Durration);
Debug.Log ("Buff Ends");
}
}
public class Buff
{
public string EffectName;
public int EffectPower;
public string EffectStat;
public float EffectDurration;
public string EffectDescription;
public Buff(string NewEffectName, int NewEffectPower, string NewEffectStat, int NewEffectDurration, string NewEffectDescription)
{
EffectName = NewEffectName;
EffectPower = NewEffectPower;
EffectStat = NewEffectStat;
EffectDurration = NewEffectDurration;
EffectDescription = NewEffectDescription;
}
}
//This is another script in another file. using UnityEngine; using System.Collections;
public class TestScript : MonoBehaviour {
void Start()
{
GameObject Hero = GameObject.Find ("Hero");
StatusEffect HeroStatScript = Hero.GetComponent<StatusEffect>();
HeroStatScript.StartBuff ("Slow", 10, "Str", 3, "Ur slowed"); ERROR SHOWS HERE!!!
}
}
Answer by hav_ngs_ru · Jan 30, 2015 at 04:12 PM
if you want to use script as Component (attach it to GameObjects and use them via GetComponent) the name of component class must be the same as filename.
here you tried to declare two nested components in one file. Of cause, GetComponent couldnt retrive StatusEffects as component.
if you want to incapsulate some data and baheviour in StatusEffects (instead of storing it directly in HeroStats members) - do it, DONT derive StatusEffects from MonoBehaviour.
public class HeroStats : MonoBehaviour
{
public class StatusEffects {
public void StartBuff(string NewEffect() {
...
}
}
public StatusEffects effects = new StatusEffects();
}
...
GameObject Hero = GameObject.Find ("Hero");
HeroStats HeroStatScript = Hero.GetComponent<HeroStats>();
HeroStatScripteffects.StartBuff (EffName, EffPow, StatAff, Durr, Descpt);
I tryed to understand ,but what do you mean by "HeroStatScripteffects.StartBuff" this part? HeroStatScripteffect doesnt exsist.? I even splited HeroStats from StatusEffect classes in diffirent files, ill update the 1st question... But changeing it didnt helped a bit tho, same error.
well, post a new code now, after you changed it - I dont know haw it looks.