NullReferenceException, Tower Defence, Video08

Updated on August 20, 2018 in  [R] Scripts
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
0 on August 20, 2018

When I hit play and click the turret button, this NullReferenceException-issue appears.

When I double click the mistake, unity is taking me to the node script.

Here is the script, pls. help, I need to ave the right script for school.

I marked the misatke line fat.

using UnityEngine;
using UnityEngine.EventSystems;

public class node : MonoBehaviour {
    public Color hovercolor;
    public Vector3 positionOffset;
    private GameObject turret;
    private Renderer rend;
    private Color startColor;
    buildManager buildManager;
   
    void Start()
    {
        rend = GetComponent<Renderer>();
        startColor = rend.material.color;
        buildManager = buildManager.instance;
    }

    void OnMouseDown()
    {
        if (EventSystem.current.IsPointerOverGameObject())
            return;

        if (buildManager.GetTurretToBuild() == null)
            return;
        

       if( turret != null)
        {
            Debug.Log(“Can´t build there – TODO: Display on screen.”);
            return;
        }
        GameObject turretToBuild = buildManager.GetTurretToBuild();
        turret = (GameObject)Instantiate(turretToBuild, transform.position+positionOffset, transform.rotation);
    }

    void OnMouseEnter()
    {
        if (EventSystem.current.IsPointerOverGameObject())
            return;

        if (buildManager.GetTurretToBuild() == null)
            return;

        rend.material.color = hovercolor;
    }

    void OnMouseExit()
    {
        rend.material.color = startColor;
    }
}

  • Liked by
Reply