I am working on a racing game, and in my racing game the player is supposed to be able to change parts that have different modifiers for the cars in the game. I have a Parts class that I added to an empty GameObject in my scene and then added that to all the cars in the slot created when I added public Parts parts; to my car script. I also have all the different part types set up as different arrays set up like this:
public Parts parts;
public Parts[] engineParts = new Parts[12];
public Parts[] turboParts = new Parts[12];
public Parts[] fuelParts = new Parts[7];
public Parts[] drivetrainParts = new Parts[3];
public Parts[] transmissionParts = new Parts[7];
public Parts[] suspensionParts = new Parts[4];
Then at the start of my Start function I take care of instantiating like this;
for (int i = 0; i < engineParts.Length; i++)
{
engineParts *= Instantiate(parts) as Parts;*
engineParts*.transform.parent = this.transform.parent;*
turboParts = Instantiate(parts) as Parts;
turboParts*.transform.parent = this.transform.parent;*
if (i < fuelParts.Length)
{
fuelParts = Instantiate(parts) as Parts;
fuelParts*.transform.parent = this.transform.parent;*
transmissionParts = Instantiate(parts) as Parts;
transmissionParts*.transform.parent = this.transform.parent;*
if (i < suspensionParts.Length)
{
suspensionParts = Instantiate(parts) as Parts;
suspensionParts*.transform.parent = this.transform.parent;*
if (i < drivetrainParts.Length)
{
drivetrainParts = Instantiate(parts) as Parts;
drivetrainParts*.transform.parent = this.transform.parent;*
}
}
}
}
The rest of my Start function is just putting the values inside each of the different variables of the arrays. The Start function doesn’t throw any exceptions, so I would think that it is running properly, however when I look at the Parts arrays inside of the inspector they still look empty and then when I try to call on the values to try and add parts to one of my cars, I get this error message in my console;
NullReferenceException: Object reference not set to an instance of an object
ShopMenu.ShopButton1 () (at Assets/Scripts/ShopMenu.cs:218)
MenuManager.SideButton1 () (at Assets/Scripts/MenuManager.cs:301)
UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/BuildAgent/work/d63dfc6385190b60/Runtime/Export/UnityEvent.cs:110)
UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/BuildAgent/work/d63dfc6385190b60/Runtime/Export/UnityEvent.cs:576)
UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/BuildAgent/work/d63dfc6385190b60/Runtime/Export/UnityEvent.cs:718)
UnityEngine.Events.UnityEvent.Invoke () (at C:/BuildAgent/work/d63dfc6385190b60/Runtime/Export/UnityEvent_0.cs:53)
UnityEngine.UI.Button.Press () (at C:/BuildAgent/work/d63dfc6385190b60/Extensions/guisystem/guisystem/UI/Core/Button.cs:36)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/BuildAgent/work/d63dfc6385190b60/Extensions/guisystem/guisystem/UI/Core/Button.cs:45)
UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/BuildAgent/work/d63dfc6385190b60/Extensions/guisystem/guisystem/EventSystem/ExecuteEvents.cs:52)
UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/BuildAgent/work/d63dfc6385190b60/Extensions/guisystem/guisystem/EventSystem/ExecuteEvents.cs:269)
UnityEngine.EventSystems.EventSystem:Update()
EDIT I have 20 cars in my game and each car has 45 parts that can be added to it, I can manually add the parts to make this work, but IF I have to add 900 Parts PreFabs to the game manually I am going to be upset, and I would prefer to do this with code. Please help me, you are my weekend’s only hope.
EDIT2 I have tried the suggested answer below, and also attempting to force script execution order so that the Parts script is handled first, followed by the Car script and then finally the CarManager. That didn’t change anything though.
EDIT3 The line of code that is actually throwing the error is;
MenuManager.Instance.itemNameText.text = TruckManager.Instance.trucks[currentTruck].engineParts[currentPart].name;
It is called based on the user pressing a uUI button. I am selecting a truck, and then going back through the menu system to get to the modification system. So it is quite a while after the Start function should have been called.