Maybe something you’re instantiating is null or some other value in that area of code. If you check that section, you may find your answer. If you’re still lost, you could post your code and someone might notice what’s potentially wrong.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BuildManager : MonoBehaviour {
public static BuildManager instance;
public GameObject LaserRiflePrefab;
public GameObject MissileLauncherPrefab;
public GameObject LaserBeamPrefab;
public TurretBlueprint TurretToBuild;
public bool CanBuild{ get { return TurretToBuild != null; } }
void Awake()
{
if(instance != null)
{
Debug.Log(“More than 1 Buildmanager”);
return;
}
instance = this;
}
public void BuildTurretOn(Node node)
{
if (PlayerStats.Money < TurretToBuild.Cost)
{
Debug.Log(“Not enough Money”);
return;
}
PlayerStats.Money -= TurretToBuild.Cost;
GameObject Turret = (GameObject)Instantiate(TurretToBuild.Prefab, node.GetBuildPosition(), Quaternion.identity);
node.CurrentTurret = Turret;
}
public void SelectTurretToBuild(TurretBlueprint turret)
{
TurretToBuild = turret;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class Node : MonoBehaviour {
public Color HoverColor;
public Vector3 PositionOffset;
[Header(“Optional”)]
public GameObject CurrentTurret;
private Renderer Rend;
private Color StartColor;
BuildManager buildManager;
void Start()
{
Rend = GetComponent();
StartColor = Rend.material.color;
buildManager = BuildManager.instance;
}
public Vector3 GetBuildPosition()
{
return transform.position + PositionOffset;
}
void OnMouseDown()
{
if (EventSystem.current.IsPointerOverGameObject())
return;
if (CurrentTurret != null)
{
return;
}
if (!buildManager.CanBuild)
{
return;
}
buildManager.BuildTurretOn(this);
}
void OnMouseEnter()
{
if (EventSystem.current.IsPointerOverGameObject())
return;
And since you are getting an error, double click it and it should open the script and take you to the proper line where the null is occurring. In this case, looks like buildManager line 33. And since it does mention instantiate, it’s probably your object that you are trying to instantiate. But again, you should easily be able to solve this by looking at that line when you double click the error and seeing what parts might be null.
it’s null because no turret is selected and that is the way it should be, and it still highlights the Square you can build on without a turret selected, but it should’n highlight it, with that i mean when you hold over a place you can build it gets Black and White Again when your not over it
Please repost the relevant code parts using code tags (see @Brathnann 's response for the link) and indicate which line has the error , to refresh anyone’s memory who is reading this
I don’t understand your game enough to really comment on if what you are doing is the right approach. But if an object must be null but you need to use it, you are going to get an error. So you need to check if the value is null before you try to use it. If you can’t do this, then your code needs some work because you can’t just have a null value that you try to use (example you can’t instantiate a null gameobject).
The only solution I can think of is you have a default gameobject that is empty and use that as a placeholder when the object needs to be null, but otherwise, checking for null should be enough.
Because you’ve made it past TurretToBuild.Cost, I’m going to assume it’s node that’s NULL.
So as @Brathnann suggested, it may be enough to just throw an error if node is null because you shouldn’t allow building on a null node. Or investigate why it’s null to begin with if “that shouldn’t happen”.