Getting error NullReferenceException on instantiate
This morning all worked so fine and know when i wanted to continue my project i'm getting an error. I work with collab and i got an error earlier and needed to chage turret to Turret to can update the project to collab. P.S. Script node.cs color the hover mouse node only if you clicked a button before that. That worked in morning but now i the hover color of node appear without you to click that button. I don't understand. I'm mind f**k... I'm getting this error:
UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, Vector3 pos, Quaternion rot) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineObjectBindings.gen.cs:51)
UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:157)
UnityEngine.Object.Instantiate[GameObject] (UnityEngine.GameObject original, Vector3 position, Quaternion rotation) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:207)
BuildManager.BuildTurretOn (.Node node) (at Assets/Scripts/BuildManager.cs:29)
Node.OnMouseDown () (at Assets/Scripts/Node.cs:35)
UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32)
Node.cs using UnityEngine; using UnityEngine.EventSystems;
public class Node : MonoBehaviour {
[Header("Optional")]
public GameObject turret;
public Color hoverColor;
public Vector3 positionOffset;
private Renderer rend;
private Color startColor;
BuildManager buildManager;
void Start()
{
rend = GetComponent<Renderer> ();
startColor = rend.material.color;
buildManager = BuildManager.instance;
}
public Vector3 GetBuildPosition()
{
return transform.position + positionOffset;
}
void OnMouseDown()
{
if (EventSystem.current.IsPointerOverGameObject ())
return;
if (!buildManager.CanBuild)
return;
if (turret != null) {
Debug.Log ("Ai creat deja o arma aici!");
return;
}
buildManager.BuildTurretOn (this);
}
void OnMouseEnter()
{
if (EventSystem.current.IsPointerOverGameObject ())
return;
if (!buildManager.CanBuild)
return;
rend.material.color = hoverColor;
}
void OnMouseExit()
{
rend.material.color = startColor;
}
}
BuildManager.cs
using UnityEngine;
public class BuildManager : MonoBehaviour {
public static BuildManager instance;
void Awake()
{
if (instance != null) {
Debug.Log ("More than one BuildManager in scene!");
return;
}
instance = this;
}
public GameObject standardTurretPrefab;
public GameObject anotherTurretPrefab;
public TurretBluePrint turretToBuild;
public bool CanBuild { get { return turretToBuild != null; } }
public void BuildTurretOn(Node node)
{
if (PlayerStats.Money < turretToBuild.cost) {
Debug.Log ("Nu ai bani destui!");
return;
}
PlayerStats.Money -= turretToBuild.cost;
GameObject turret = (GameObject)Instantiate (turretToBuild.prefab, node.GetBuildPosition(), Quaternion.identity);
node.turret = turret;
Debug.Log("Turret build! Money left: " + PlayerStats.Money);
}
public void SelectTurretToBuild(TurretBluePrint turret)
{
turretToBuild = turret;
}
}
TurretBluePrint.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class TurretBluePrint{
public GameObject prefab;
public int cost;
}
The errors from scripts:
BuildManager.cs Line 29:
GameObject turret = (GameObject)Instantiate (turretToBuild.prefab, node.GetBuildPosition(), Quaternion.identity);
Node.cs Line 35:
buildManager.BuildTurretOn (this);
maybe something went wrong with the serialisation. This kind of error doesn't look normal. I'd assign prefab to turretToBuild again.
Is this right?
public class TurretBluePrint
Should this have : $$anonymous$$onoBehaviour on it?
Answer by Al3xQQ · Mar 30, 2017 at 04:21 PM
@hexagonius How and where should i do that?
@Rhombuster No, that's how shoud be. https://youtu.be/pZ0QyngaQv4?t=1m31s
Answer by MarnixM · Apr 02, 2017 at 03:55 PM
so i have the same idea for a game like you do. have u checked the shop script in the inspector? because you might have failed the fill in the prefabs for standardTurret and missileTurret. i had the same error and after checking everything and remaking all the scripts, I found out that it had to do with the blank space under the shop script in the inspector. just fill in these spots with the prefabs and your game will work again.
Answer by Shiner252 · Jul 24, 2017 at 09:57 PM
I had the same problem. I might be late, but I will answer in case someone stumbles upon this while searching for a solution. In your Node.cs script, make sure you define the instance of the Build Manager like so:
void Start(){ buildManager = BuildManager.instance }