Tower Defence EP(08)

Updated on March 25, 2019 in [A] Tutorials
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
11 on March 19, 2019

 Been trying for hours to correct it but no luck keep getting errors on the same four lines of code but whatever i do it never changes.

Under Node Tab:

— void OnMouseEnter()
{
if (buildManager.GetTurretToBuild() == null)
return;

— void OnMouseDown()
{
if (buildManager.GetTurretToBuild() == null)
return;

Under Shop Tab;

— buildManager.SetTurretToBuild(buildManager.standardTurretPrefab);

— buildManager.SetTurretToBuild(buildManager.anotherTurretPrefab);

Node Tab Code:

— 

using UnityEngine;

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 (buildManager.GetTurretToBuild() == null)
return;

if (turret !=null) 
{
Debug.Log(“CANNOT PLACE HERE!”);
return;
}

GameObject turretToBuild = buildManager.GetTurretToBuild(); 
turret = (GameObject)Instantiate(turretToBuild, transform.position + positionOffset, transform.rotation);

}

void OnMouseEnter()
{
if (buildManager.GetTurretToBuild() == null)
return;

rend.material.color = hoverColor; 
}

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

Shop Tab Code:

— 

using UnityEngine;

public class shop : MonoBehaviour
{
BuildManager buildManager;

void Start()
{
buildManager = BuildManager.instance;

}

public void PurchaseStandardTurret()
{
Debug.Log(“Standard Turret Purchased”);
buildManager.SetTurretToBuild(buildManager.standardTurretPrefab);
}

public void PurchaseAnotherTurret()
{
Debug.Log(“Another Turret Purchased”);
buildManager.SetTurretToBuild(buildManager.anotherTurretPrefab);
}
}

Any help will be greatly appreciated,

Thanks!!

 

  • Liked by
Reply
4 on March 19, 2019

Errors are:

— NullReferenceException: Object reference not set to an instance of an object
shop.PurchaseAnotherTurret () (at Assets/Scripts/shop.cs:24)

— NullReferenceException: Object reference not set to an instance of an object
Node.OnMouseEnter () (at Assets/Scripts/Node.cs:45)
UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32)

— NullReferenceException: Object reference not set to an instance of an object
shop.PurchaseStandardTurret () (at Assets/Scripts/shop.cs:18)

Wise
on March 20, 2019

The errors are telling you what script they’re in, and what line they’re on.

Shop, line 18 and 24

Node, line 45

 

For the future, please use the “[ code] [/code ]” tags when pasting your code. It makes it much easier to read, and we could see the line numbers to make errors like this easier to troubleshoot.

on March 22, 2019

i will use the tags next time i have a problem.

I know that it tells me the what script the errors are in but i have no idea what they errors are because to me everything looks fine and should work.

Wise
on March 22, 2019

If I had to guess,

it looks like it’s your BuildManager script.

Can we see your BuildManager script?

 

on March 24, 2019

Build Manager Script

 

using UnityEngine;
public class BuildManager : MonoBehaviour
{
public static BuildManager instance;
 public GameObject standardTurretPrefab;
 public GameObject anotherTurretPrefab;
private GameObject turretToBuild;
public GameObject GetTurretToBuild()
 {
 return turretToBuild;
 }
public void SetTurretToBuild(GameObject turret)
 {
 turretToBuild = turret;
 }
}
Show more replies
  • Liked by
Reply
Cancel
5 on March 24, 2019

i have changed some things in my code and i have gotten rid of two errors, the only error i get know is when i move my mouse up and down:

— NullReferenceException: Object reference not set to an instance of an object
Node.OnMouseEnter () (at Assets/Scripts/Node.cs:45)
UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32)

Wise
on March 25, 2019

So it looks like your build manager is supposed to be a singleton, but you’re missing the parts that make sure instance is always set to something, or that removes other BuildManagers.

This could be what was causing the null refs in in the shop script, and what may be causing it now.

 

If instance is never being set, then

if (buildManager.GetTurretToBuild() == null)

Would break instantly, and not return false like you would expect it to/ want it to here.

 

Try this for your BuildManager  and see if you still get null errors.

using UnityEngine;
public class BuildManager : MonoBehaviour
{
    private static BuildManager _instance;
    public static BuildManager instance
    {
        get
        {
            if(_instance == null)
            {
               _instance = FindObjectOfType<BuildManager>();
            }
            return _instance;
        }
    }
    
    void Start()
    {
        if(instance != this) Destroy(gameObject);
    }
    public GameObject standardTurretPrefab;
    public GameObject anotherTurretPrefab;
    private GameObject turretToBuild;
    public GameObject GetTurretToBuild()
    {
        return turretToBuild;
    }
    public void SetTurretToBuild(GameObject turret)
    {
        turretToBuild = turret;
    }
}
on March 25, 2019

Thankyou, no errors now in the console, but when i go to click on a turret and try to click on a node to place it doesnt let me place down the turret

 

on March 25, 2019

But of tweaking and the code now runs, but the major problem is that i have to turrets (StandardTurret and AnotherTurret) and i can place the standard turret fine but if i try to place AnotherTurret first it lets me click it can i get the message “AnotherTurret Bought” but when i try to place it, it wont place. But when i but the standard turret first and then Anotherturret it lets me place both but defaults the AnotherTurret to a Standard turret but says i have bought a “Standard Turret” and “Another Turret”.

 

sorry if this makes no sense, if it doesnt tell me and ill re word it and make it more understandable 

Wise
on March 25, 2019

I’m not sure what could be causing that.

Sorry.

on March 25, 2019

thats fine, you help was greatly appreciated and i wish you the very best, cheers!!

 

Show more replies
  • Liked by
Reply
Cancel