I have a DontDestroyOnLoad Object. When it “awakens” for the first time I set values into these variables:
public ItemAttributes equippedHead;
public ItemAttributes equippedBody;
public ItemAttributes equippedLegs;
public ItemAttributes equippedArms;
public ItemAttributes equippedWeapon
I can then access the values. However, once the scene changes the game object does indeed remain, but when I try and access the above variables, I get a null reference exception. This leads me to believe that these values are being wiped out. The above variables are of type ItemAttributes, which is a class. They are obviously filled with objects instances of the ItemAttributes class.
There is no code of mine that clears out these values.
is ItemAttributes a GameObject/Object?
in that case you should set the “don’t destroy on load” on all of them too otherwise they will get destroyed on a new scene load.
No. ItemAttributes is simply a class script.
Here is my code. You’ll notice in my update method I am trying to access the culprit class member. When the game first loads, the debug.log shows the value of equippedHead.SetID which is 0 (as it should be). When the new scene loads, I get a null reference expection on that debug line.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlayerAttributes : MonoBehaviour
{
//List of player inventory by item name
public List<ItemAttributes> Inventory = new List<ItemAttributes>();
public List<int> inventoryIndexesRunning = new List<int>();
//Object reference declarations
ItemList itemList;
//Basic Attributes
float attackModifier;
float defenseModifier;
float luckModifier;
//Base skill levels (these will increase as the player gains levels)
public int BaseStrength;
public int BaseDefense;
public int BaseLuck;
public int TotalHp;
//Current attributes with weapon modifiers
public int currentStrength;
public int currentDefense;
public int currentLuck;
//Weapon Skill Attributes
int shortWeaponLevel;
int projectileWeaponLevel;
int longWeaponLevel;
int meleeLevel;
int shortWeaponModifier;
int projectileWeaponModifier;
int longWeaponModifier;
int meleeModifier;
public ItemAttributes equippedHead;
public ItemAttributes equippedBody;
public ItemAttributes equippedLegs;
public ItemAttributes equippedArms;
public ItemAttributes equippedWeapon;
//Player level modifier values
//Indexes 1,2,3 represent levels 1,2,3 modifier values
private float[] modifierValues = new float[] {0f,15f,30f,50f};
#region Player Level Increases
#endregion
//Equip items from the armory menu
public void EquipItems(int armorType,int id)
{
//Index: 0 = head; 1 = body; 2 = legs; 3 = arms; 4 = weapon;
if (armorType == 0)
{
equippedHead = Inventory[id];
}
else if (armorType == 1)
{
equippedBody = Inventory[id];
}
else if (armorType == 2)
{
equippedLegs = Inventory[id];
}
else if (armorType == 3)
{
equippedArms = Inventory[id];
}
else if (armorType == 4)
{
equippedWeapon = Inventory[id];
}
}
public void UpdateAttributes()
{
currentStrength = BaseStrength;
currentDefense = BaseDefense;
currentLuck = BaseLuck;
int tempStrength = 0;
tempStrength += equippedHead.Strength;
tempStrength += equippedBody.Strength;
tempStrength += equippedLegs.Strength;
tempStrength += equippedArms.Strength;
//tempStrength += equippedWeapon.Strength;
currentStrength += tempStrength;
int tempDefense = 0;
tempDefense += equippedHead.Defense;
tempDefense += equippedBody.Defense;
tempDefense += equippedLegs.Defense;
tempDefense += equippedArms.Defense;
//tempDefense += equippedWeapon.Defense;
currentDefense += tempDefense;
int tempLuck = 0;
tempLuck += equippedHead.Luck;
tempLuck += equippedBody.Luck;
tempLuck += equippedLegs.Luck;
tempLuck += equippedArms.Luck;
//tempLuck += equippedWeapon.Luck;
currentLuck += tempLuck;
}
public ItemAttributes AddInventory
{
set
{
Inventory.Add(value);
}
}
public int AddInventoryIndex
{
set
{
inventoryIndexesRunning.Add(value);
}
}
void Awake()
{
DontDestroyOnLoad(this);
}
void Start()
{
itemList = (ItemList)gameObject.GetComponent(typeof(ItemList));
//Add base character
AddInventory = itemList.attributes[0];
AddInventory = itemList.attributes[1];
AddInventory = itemList.attributes[2];
AddInventory = itemList.attributes[3];
//Add base items
equippedHead = Inventory[0];
equippedBody = Inventory[1];
equippedLegs = Inventory[2];
equippedArms = Inventory[3];
for (int x = 4; x < itemList.attributes.Length;x++)
{
Inventory.Add(itemList.attributes[x]);
}
}
void Update()
{
UnityEngine.Debug.Log(equippedHead.SetID);
}
class script you mean a new class defined inside another script?
have you wrote that script in c# or js?
because with js when you create a new file unity will automatically create the class definition where the class name == filename and it extends automatically the MonoBheaviour class (and going up the Object class) so in that case you have to use the DontDestroyOnLoad
otherwise can you post your code?
edit:
ok seen your code 
can you post the ItemAttributes file/definition?
ItemAttributes Class:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ItemAttributes : MonoBehaviour
{
public int ID;
public int SetID;
public string ItemSet;
public string ItemType;
public int Defense;
public int Luck;
public int Strength;
}
yep it extends MonoBehaviour so you’ll need to use DontDestroyOnLoad on all the references that you want to keep (you can call it in PlayerAttribute.Start() method)
So even if this class isn’t on a game object I need to call DontDestroyOnLoad on it? Its just a class that sits in my scripts folder.
the base class in unity is Object not GameObject and MonoBehaviour inherit from it.
If you check the documentation you’ll see that DontDestroyOnLoad is an Object method not a GameObject one.
In your case anyway seems like you don’t need any of the MonoBehaviour features (you don’t do anything on Start/Update etc.) so you can simply remove the “: MonoBehaviour” from your ItemAttributes class definition and everything should work just fine as it is.
I removed its inheritance and it works great now. I appreciate the help. Learn new stuff every day!
Hmm, stripping the MonoBehavior inheritance is causing this(i think) to not work on the iphone. Do I have to do anything special to get it to function?
I just debugged and confirmed, the script is the problem. How do I proceed?