- Home /
Question by
TankadiN · Apr 28, 2019 at 04:55 PM ·
c#scripting problemscript error
Getting Null Reference Exception on working Script ?
I want to get data from a gameobjects to save, but when i try getting data it tells: NullReferenceException: Object reference not set to an instance of an object LevelData.GetData () (at Assets/Scripts/LevelData.cs:25)
But when I'm inspecting gameobject where script is attached, it works perfectly. I don't know if it's my mistake or bug but i'll be glad if someone help me.
LevelData.cs
using System.Collections;
using UnityEngine;
public class LevelData : MonoBehaviour
{
public static LevelData LD;
public PlaceInfo[] Places;
public GameObject[] targetplace;
private int i = 0;
private IEnumerator GetPlaces()
{
targetplace = GameObject.FindGameObjectsWithTag("Place");
Places = new PlaceInfo[targetplace.Length];
yield return new WaitForSeconds(0.5f);
GetData();
}
public void GetData()
{
foreach (GameObject o in targetplace)
{
Places[i].rectX = o.GetComponent<RectTransform>().rect.x;
Places[i].rectY = o.GetComponent<RectTransform>().rect.y;
Places[i].rectWidth = o.GetComponent<RectTransform>().rect.width;
Places[i].rectHeight = o.GetComponent<RectTransform>().rect.height;
if (o.GetComponent<Place>())
{
Places[i].PlaceType = PlaceInfo.Type.Place;
Places[i].SilverLock = o.GetComponent<Place>().SilverLock;
Places[i].GoldLock = o.GetComponent<Place>().GoldLock;
Places[i].ProgressNeeded = o.GetComponent<Place>().ProgressNeeded;
Places[i].EnergyNeeded = o.GetComponent<Place>().EnergyNeeded;
Places[i].MinExpOngoing = o.GetComponent<Place>().MinExpOngoing;
Places[i].MaxExpOngoing = o.GetComponent<Place>().MaxExpOngoing;
Places[i].MinGoldOngoing = o.GetComponent<Place>().MinGoldOngoing;
Places[i].MaxGoldOngoing = o.GetComponent<Place>().MaxGoldOngoing;
Places[i].MinGoldCompleted = o.GetComponent<Place>().MinGoldCompleted;
Places[i].MaxGoldCompleted = o.GetComponent<Place>().MaxGoldCompleted;
Debug.Log("Places Fetched");
}
i++;
}
i = 0;
Debug.Log("Done Succesfully.");
}
public void GetPlacesButton()
{
StartCoroutine(GetPlaces());
}
}
PlaceInfo.cs using System.Collections; using System.Collections.Generic; using UnityEngine;
[System.Serializable]
public class PlaceInfo
{
public float rectX, rectY, rectWidth, rectHeight;
public enum Type {Place, Enemy, Shop, Jackpot}
public Type PlaceType;
public bool GoldLock, SilverLock;
public float ProgressNeeded;
public float EnergyNeeded;
public float MinExpOngoing, MaxExpOngoing;
public float MinGoldOngoing, MaxGoldOngoing;
public float MinGoldCompleted, MaxGoldCompleted;
public float MaxHealth, EnemyAttack;
public float MinSKeys, MaxSKeys, MinGKeys, MaxGKeys;
public float Cost, Value;
}
Comment
Your answer
Follow this Question
Related Questions
[C#] Quaternion Rotations with Input.GetAxis problems. 1 Answer
(Problem) Turning postprocessing on and off 0 Answers
Visual Studio Code Intellisense 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers