I have this list which contains values for each property in the game. But when i add a value and try to access it it says “Object reference not set to an instance of an object”.
This is not the whole code but i have something like 800 lines of code that is irrelevant so i added only the important parts. I have another script that adds Properties to player with playerprefs. I am really lost right now don’t know whats wrong
public string propertiesName { get; set; }
public int propertiesPrice { get; set; }
public string propertiesAddress { get; set; }
public int propertiesAge { get; set; }
public int propertiesRooms { get; set; }
public int propertiesBathrooms { get; set; }
public string propertiesLocation { get; set; }
public int propertiesMonthlyExpense { get; set; }
public int propertiesCondition { get; set; }
int propertiesAmount = 0;
public PropertiesSc(string propertiesNameX, int propertiesPriceX, string propertiesAddressX, int propertiesAgeX, int propertiesRoomsX, int propertiesBathroomsX, string propertiesLocationX, int propertiesMonthlyExpenseX, int propertiesConditionX)
{
propertiesName = propertiesNameX;
propertiesPrice = propertiesPriceX;
propertiesAddress = propertiesAddressX;
propertiesAge = propertiesAgeX;
propertiesRooms = propertiesRoomsX;
propertiesBathrooms = propertiesBathroomsX;
propertiesLocation = propertiesLocationX;
propertiesMonthlyExpense = propertiesMonthlyExpenseX;
propertiesCondition = propertiesConditionX;
}
public void SetAssetsProperties()
{
isPropertyAssetActive = true;
propertiesAmount = PlayerPrefs.GetInt("PropertiesAmount");
for (int i = 0; i < propertiesAmount; i++)
{
string name = PlayerPrefs.GetString("Property" + (i+1).ToString() + "Name");
int price = PlayerPrefs.GetInt("Property" + (i+1).ToString() + "Price");
string address = PlayerPrefs.GetString("Property" + (i+1).ToString() + "Address");
int age = PlayerPrefs.GetInt("Property" + (i+1).ToString() + "Age");
int rooms = PlayerPrefs.GetInt("Property" + (i+1).ToString() + "Rooms");
int bathrooms = PlayerPrefs.GetInt("Property" + (i+1).ToString() + "Bathrooms");
string location = PlayerPrefs.GetString("Property" + (i+1).ToString() + "Location");
int monthlyExpense = PlayerPrefs.GetInt("Property" + (i+1).ToString() + "MonthlyExpense");
int condition = PlayerPrefs.GetInt("Property" + (i+1).ToString() + "Condition");
PlayerPrefs.SetInt("Property" + (i+1).ToString() + "Number", i);
var propertyInfo = new PropertiesSc(name, price, address, age, rooms, bathrooms, location, monthlyExpense, condition);
propertiesList.Add(propertyInfo);
}
}
public void SetPlayerPrefsProperties()
{
PlayerPrefs.SetInt("PropertiesAmount", propertiesAmount);
for (int i = 0; i < propertiesAmount; i++)
{
PlayerPrefs.SetString("Property" + (i+1).ToString() + "Name", propertiesList[i].propertiesName);
PlayerPrefs.SetInt("Property" + (i+1).ToString() + "Price", propertiesList[i].propertiesPrice);
PlayerPrefs.SetString("Property" + (i+1).ToString() + "Address", propertiesList[i].propertiesAddress);
PlayerPrefs.SetInt("Property" + (i+1).ToString() + "Age", propertiesList[i].propertiesAge);
PlayerPrefs.SetInt("Property" + (i+1).ToString() + "Rooms", propertiesList[i].propertiesRooms);
PlayerPrefs.SetInt("Property" + (i+1).ToString() + "Bathrooms", propertiesList[i].propertiesBathrooms);
PlayerPrefs.SetString("Property" + (i+1).ToString() + "Location", propertiesList[i].propertiesLocation);
PlayerPrefs.SetInt("Property" + (i+1).ToString() + "MonthlyExpense", propertiesList[i].propertiesMonthlyExpense);
PlayerPrefs.SetInt("Property" + (i+1).ToString() + "Condition", propertiesList[i].propertiesCondition);
}
}
You really shoul show all relevant code, as it’s hard to piece together the info when you are hiding it. What is the definition of PropertiesSc? In any event, it looks like propertiesList isn’t initialized or contains a null entry. Log i when you access it so you know which entries are iterated successfully, and which dont.
This doesn’t line up with when you said the error is on line 54 in your previous post, as there’s nothing on line 54 here.
Can you please post the exact error message?
For 100% of cases where you get this particular error, something in that line is null. Simply debug each variable involved until you work backwards and resolve the issue.
TBH: I recommend scrapping it. PlayerPrefs is AWFUL for saving anything more complicated than a single primitive type, and one of the reasons why it because you’ll run into errors like this time and time again.
Incorporate a serializer instead - it will literally be easier to learn how to do this than it will be to debug your PlayerPrefs code. You won’t have to have a line of code saving every variable, looping through every item in an array and painstakingly constructing a unique string identifier for each one… you just have all your data in a class, mark that class as [Serializable], and then tell the serializer “put this thing in that file” and you’re done. Here’s a tutorial for doing that with a binary file format, here’s one for saving with JSON.
Note also that you are using a constructor for PropertiesSc with a MonoBehaviour, which (at least some time ago) was seriously discouraged. Log to see if the constructor is invoked.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class testscript : MonoBehaviour
{
public int priceHome { get; set; }
public string nameHome { get; set; }
public int ageHome { get; set; }
// Start is called before the first frame update
public void SetValues(int priceHomeX, string nameHomeX, int ageHomeX)
{
priceHome = priceHomeX;
nameHome = nameHomeX;
ageHome = ageHomeX;
}
void Awake()
{
SetValues();
}
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
We all love to help, but you really should be specific when asking for help. Remember that we did not look over your shoulder when this happened, so assume that we know nothing So:
What error did you get (copy and paste entire error message), and
where did it occur (make sure the line number matches up, allow for formatting changes in code tags)?
I tried this and it worked. but when i tried to create a list with SetValues it gives this error:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class testscript : MonoBehaviour
{
public int priceHome { get; set; }
public string nameHome { get; set; }
public int ageHome { get; set; }
// Start is called before the first frame update
public void SetValues(int priceHomeX, string nameHomeX, int ageHomeX)
{
priceHome = priceHomeX;
nameHome = nameHomeX;
ageHome = ageHomeX;
}
List<SetValues> homeList = new List<SetValues>();
void Awake()
{
SetValues(priceHome, nameHome, ageHome);
}
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
Assets/testscript.cs(19,10): error CS0246: The type or namespace name ‘SetValues’ could not be found (are you missing a using directive or an assembly reference?)