0

I am trying to make a tower defense game (just to learn unity) but whenever i try to build my Missile Launcher it gives me this error which leads to my build manager on this specific line:

    GameObject turret = (GameObject)Instantiate(turretToBuild.prefab, node.GetBuildPosition(), Quaternion.identity);
    node.turret = turret;

Here is the rest of the code;

using UnityEngine;

public class BuildManager : MonoBehaviour
{

 public static BuildManager instance;

 void Awake ()
 {
    if (instance !=null)
    {
        Debug.LogError("More than one BuildManager in scene!");
        return;
    }
    instance = this;
 }

 public GameObject standartTurretPrefab;
 public GameObject missileLauncherPrefab;

 public GameObject buildEffect;

 private TurretBlueprint turretToBuild;

 public bool CanBuild { get { return turretToBuild != null; } }
 public bool HasMoney { get { return PlayerStats.Money >= turretToBuild.cost; } }

 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.turret = turret;

    GameObject effect = (GameObject)Instantiate(buildEffect, node.GetBuildPosition(), Quaternion.identity);
    Destroy(effect, 5f);

    Debug.Log("Turret build! Money left: " + PlayerStats.Money);
 }


 public void SelectTurretToBuild (TurretBlueprint turret)
 {
    turretToBuild = turret; 
 }

}

Any help is appreciated.

3Dave
  • 26,903
  • 17
  • 82
  • 145
  • 4
    So have you debugged into your code to find if `node` is null, for example? – Jon Skeet Jun 12 '20 at 14:36
  • Either `turretToBuild` or `node` are `null` when that line is executed. Set some breakpoints and trace. – 3Dave Jun 12 '20 at 14:47
  • Actually, it can't be `turretToBuild`. So, `node` it is. – 3Dave Jun 12 '20 at 14:50
  • Sorry for not implementing in the question tab but i have already made scripts for node, enemy, missile launcher etc... and i didn't specify my problem.@JonSkeet it does give me this error after i clicked node with the missile launcher.I have also another turret and it works just fine.The enemies spawns as normal.My other turret(standart turret) works quite excellent with the node but when i try to put the missile launcher on the node.After i click the node with my missile launcher selected, it gives me this error. – YellowMellow Jun 17 '20 at 14:59

0 Answers0