Object reference error
Hi, I'm getting this error with this script:
NullReferenceException: Object reference not set to an instance of an object WorldUI.Start () (at Assets/scripts/WorldUI.cs:17)
I've read, tha this occurs, if The calss Buildings doesn't contain MenuIcon, but as you can see, the script includes sthis.
WorldUI:
using UnityEngine;
using System.Collections;
public class WorldUI : MonoBehaviour {
void Start () {
string path = "Buildings/";
Object[] Buildings = Resources.LoadAll(path);
if(Buildings.Length > 0) {
for(int i =0; i < Buildings.Length; i++)
{
GameObject build = Buildings[i] as GameObject;
Texture2D Icon = build.GetComponent<Building>().MenuIcon; =====LINE 17=====
// Texture2D IconRo = build.GetComponent<Building>().MenuIconRo;
// Debug.Log(Buildings[i]);
MenuSetup.UnitIconTextures.Add(Icon);
// MenuSetup.UnitIconTexturesRo.Add(Icon);
MenuSetup.UnitNames.Add(build.name);
MenuSetup.UnitPaths.Add(path+"/"+build.name);
}
}
}
}
Building
using UnityEngine;
using System.Collections;
public class Building : MonoBehaviour {
public Vector2 ScreenPos;
public bool OnScreen;
public float team; // 0 = spieler; 1 = gegner
public bool Selected = false;
public float HP = 100f;
public float Reload = 10f;
public float Range = 40f;
public bool Alive = true;
public bool Selectable = true;
public Texture2D MenuIcon; ======HERE=======
public Texture2D MenuIconRo;
}
I hope you can help me here
Well, I'm assu$$anonymous$$g that you have a physical instance of the Building class, but you still have to assign a menu icon to that class variable in the inspector/through code. Simply declaring a variable will not give it a value.
Yep, I did that already, forgot to upload a picture of the Inspector. The image however is set there
Answer by relativ · Sep 20, 2016 at 09:56 AM
Geez....The problem was; the Pictures were Sprites, not GUI Textures ._.
Your answer
Follow this Question
Related Questions
Can't build my Project 1 Answer
Object reference not set to an instance of an object 0 Answers
Instatiated object not being referenced in Start function? 2 Answers
[HELP] Following the 2D character controller tutorial from the live training section (C#) 0 Answers
[c sharp] why isn't my player dying when it touches the truck? 1 Answer