Need help with if else statement...


void OnMouseDown ()
	if (turret != null) 
		Debug.Log("Turret already placed here.");
		if (turret == buildManager.standardTurretPrefab) {
			Debug.Log ("This is a Standard Turret.");
		} else

Build Manager:

   using UnityEngine;

   public class BuildManager : MonoBehaviour {

public static BuildManager instance; 

void Awake ()
	instance = this;

public GameObject standardTurretPrefab;
public GameObject SateliteTurretPrefab;
public GameObject MissileLauncherPrefab;
public GameObject DrillGoldDiggerPrefab;
public GameObject TeslaCoilPrefab;
public MoneyManager moneyManager;

private TurretBlueprint turretToBuild;

public bool CanBuild{ get { return turretToBuild != null;}}

public void SelectTurretToBuild(TurretBlueprint turret)
	turretToBuild = turret;

public void BuildTurretOn(Node node)
	if ( <= turretToBuild.cost) 
		Debug.Log ("Not enough money");	
	} -= (turretToBuild.cost);
	GameObject turret = (GameObject)Instantiate (turretToBuild.prefab, node.GetBuildPosition (), Quaternion.identity);
	node.turret = turret;


Why does this not work? Do I need to check for the name of the object in stead of the classname? (StandardTurretPrefab(Clone))?

If you are comparing two gameobjects it checks also it’s ObjectID that is different for each object, so you have to came up with different option of comparing objects:

  • One of them is as you figured comparing names.

  • You can also create class for each important object and check by that.

  • Creating a parent for each group of the same objects and checking what is parents name also will do the job in some cases.

All in all, the simples way is as you said by comparing names. It usually does the job in simple cases.

You are checking in instance of the class and you probably assigned the standardTurretPrefab in original class this means you are getting a NULL always … either reference your BuildManager with GetComponent or assign your members in instance also