#list
1 messages · Page 1 of 1 (latest)
Hey
what about printing shelfdatalist?
i know
ive had a couple guys try to help, but no luck unfortunatly
im that guy
huh?
ok its non relevant
Did you log the three variables Digi said could be null? 🤔
Log and check their value before the error line . . .
they arnt variables
the only variable is the list
and thats whats returning null
but no idea why
Then you know the issue; it's null. Assign it a value . . .
You're tryna access the list but it's null, it has not been initialized or assigned a value . . .
You gotta know how to initialize a list. If not, Google how to . . .
even if the list is 0, the line is to add onto the list
Yeah I know how google works, I wouldent be here if i hadnt already tried
It's not zero, it doesn't exist yet . . .
the dataList is in the Json script, this would just return an error
You don't need a parameter. They could've easily Googled that though . . .
you know that for a list to have any values it needs to be created
?
aka initialized
don't confuse with declare
yes
Yeah of course I have this
so it would still make a error if you changed the line to
if(shelfDataList != null) SaveLoad.gameData.shelfDataList.Add(new ShelfData());
didnt think of this, so worth a shot to try
The list is not null
it seems to be the script that is null (ShelfData) or something within it
ok so i have another idea
var e = new ShelfData();
SaveLoad.gameData.shelfDataList.Add(e);```
still null reference
which line?
position = new float[3] { tableScript.transform.position.x, tableScript.transform.position.y, tableScript.transform.position.z };```
Which is weird because why would this line return null
makes no sense
time to check if position, or tablescript
Hmmm, you said shelfData wasn't null . . .
I said the list wasnt null
ok it could be the protection levels too, try making tablescript public too
At this point, log every reference variable you try to access before the error to get a clear picture of what is null . . .
can you send the script again
i will modify it
[System.Serializable]
public class ShelfData
{
public float[] position;
public float[] rotation;
public int appleCount;
private ShelfData shelfData;
public Shelf tableScript;
// To be called to serialize the table script into json
public ShelfData()
{
position = new float[3] { tableScript.transform.position.x, tableScript.transform.position.y, tableScript.transform.position.z };
rotation = new float[3] { tableScript.transform.eulerAngles.x, tableScript.transform.eulerAngles.y, tableScript.transform.eulerAngles.z };
appleCount = tableScript.applesAdded;
}
// To be called when deserializing the table from json
public void ApplyTo() {
tableScript.transform.position = new UnityEngine.Vector3(position[0], position[1], position[2]);
tableScript.transform.eulerAngles = new UnityEngine.Vector3(rotation[0], rotation[1], rotation[2]);
tableScript.applesAdded = appleCount;
}
}
thanks
// To be called to serialize the table script into json
public ShelfData()
{
Debug.Log(position);
Debug.Log(rotation);
Debug.Log(tableScript);
Debug.Log(appleCount);
position = new float[3] { tableScript.transform.position.x, tableScript.transform.position.y, tableScript.transform.position.z };
rotation = new float[3] { tableScript.transform.eulerAngles.x, tableScript.transform.eulerAngles.y, tableScript.transform.eulerAngles.z };
appleCount = tableScript.applesAdded;
Debug.Log(position);
Debug.Log(rotation);
Debug.Log(tableScript);
Debug.Log(appleCount);
}
there
?
fixed it
i cant log anything because its a serialised class tho
ok well do null checks
btw tablescript is null by default
Its ok, im going to wipe eveyrthing and start from scratch
cause the class was just made
so if you were to make a new vector3
every value will be 0
but here you have a class reference and not a float
so its default value will be null
@tidal wave
so how should i set it ?
public Shelf tableScript = new Shelf();```?
ok thats weird
yeah that could work, every value of shelf will be the default tho
let me guess
hmm
wait, or maybe the transform is null,
i need to figure out how to change the transfor before adding to list
its trying to acess a non existing transform
from the new Shelf();
so
do this
// To be called to serialize the table script into json
public ShelfData()
{
if(tableScript != null) {
position = new float[3] { tableScript.transform.position.x, tableScript.transform.position.y, tableScript.transform.position.z };
rotation = new float[3] { tableScript.transform.eulerAngles.x, tableScript.transform.eulerAngles.y, tableScript.transform.eulerAngles.z };
appleCount = tableScript.applesAdded;
}
}
@tidal wave then by having tablescript public
wait nvm
could just use a parameter called newtablescript
or make this method different and not be called with new
you choose
@tidal wave
[System.Serializable]
public class ShelfData
{
public float[] position;
public float[] rotation;
public int appleCount;
private ShelfData shelfData;
public Shelf tableScript;
// To be called to serialize the table script into json
public void SetData()
{
position = new float[3] { tableScript.transform.position.x, tableScript.transform.position.y, tableScript.transform.position.z };
rotation = new float[3] { tableScript.transform.eulerAngles.x, tableScript.transform.eulerAngles.y, tableScript.transform.eulerAngles.z };
appleCount = tableScript.applesAdded;
}
// To be called when deserializing the table from json
public void ApplyTo() {
tableScript.transform.position = new UnityEngine.Vector3(position[0], position[1], position[2]);
tableScript.transform.eulerAngles = new UnityEngine.Vector3(rotation[0], rotation[1], rotation[2]);
tableScript.applesAdded = appleCount;
}
}```
yh im trying to see if I can change the transform from another script before I call add but cant seem to access it
[System.Serializable]
public class ShelfData
{
public float[] position;
public float[] rotation;
public int appleCount;
private ShelfData shelfData;
public Shelf tableScript;
// To be called to serialize the table script into json
public ShelfData(newTable)
{
tableScript = newTable;
position = new float[3] { tableScript.transform.position.x, tableScript.transform.position.y, tableScript.transform.position.z };
rotation = new float[3] { tableScript.transform.eulerAngles.x, tableScript.transform.eulerAngles.y, tableScript.transform.eulerAngles.z };
appleCount = tableScript.applesAdded;
}
// To be called when deserializing the table from json
public void ApplyTo() {
tableScript.transform.position = new UnityEngine.Vector3(position[0], position[1], position[2]);
tableScript.transform.eulerAngles = new UnityEngine.Vector3(rotation[0], rotation[1], rotation[2]);
tableScript.applesAdded = appleCount;
}
//example of use
var e = new ShelfData(Example);
}```
you can't acess it cause you are creating and then trying to reference it
i would use this
do
var e = new ShlefData();
e.tableScript = anything;
e.SetData();
@tidal wave
yeah im trying to figure it out but i really dont know, im so tired
I think im just going to delete it all and restart
why not come back another day
focus on other things
Yeah, but cant focus on other things as this mechanic ive been trying to add is the most important
oh well im done, C# wins
appreciate you taking so much time to help
np, im extremely bored, this was fun
Yeah true, but i guess im one of those people that just cant leave something unfinished
severe OCD
haha im glad you had fun
Your ShelfData class has a private ShelfData variable. Private variables need to be initialized or assigned a value. Maybe that is the issue . . .
ive re-written everything
[System.Serializable]
public class ShelfData
{
public float[] position;
public float[] rotation;
public int appleCount;
// To be called to serialize the table script into json
public ShelfData(Shelf tableScript)
{
position = new float[3] { tableScript.transform.position.x, tableScript.transform.position.y, tableScript.transform.position.z };
rotation = new float[3] { tableScript.transform.eulerAngles.x, tableScript.transform.eulerAngles.y, tableScript.transform.eulerAngles.z };
appleCount = tableScript.applesAdded;
}
// To be called when deserializing the table from json
public void ApplyTo(Shelf tableScript) {
tableScript.transform.position = new UnityEngine.Vector3(position[0], position[1], position[2]);
tableScript.transform.eulerAngles = new UnityEngine.Vector3(rotation[0], rotation[1], rotation[2]);
tableScript.applesAdded = appleCount;
}
}
The only thing I cant get working is this
SaveLoad.gameData.shelfDataList = SaveLoad.gameData.shelfDataList.ConvertAll(tableScript => new ShelfData(ShelfData));```
Is the list initialized or populated? Are you re-assigning it to itself . . .