Skip to main content
deleted 53 characters in body
Source Link
Philipp
  • 121.5k
  • 28
  • 261
  • 342

What I changed in the current script is these. ---v It worked all fine until I tried to add a menu that opens up and then you can choose a tower type you can buy.

So after I add all of this I get a NullReferenceExpectionNullReferenceExpection at "return tower == null && gameManager.Gold >= cost;"return tower == null && gameManager.Gold >= cost; and at "gameManager.Gold -= tower.GetComponent().CurrentLevel.cost;"gameManager.Gold -= tower.GetComponent<TowerData>().CurrentLevel.cost;. The game also doesn't take any money from purchases and you can spawn near infinite amounts of towers on one spot.

What I changed in the current script is these. ---v It worked all fine until I tried to add a menu that opens up and then you can choose a tower type you can buy.

So after I add all of this I get a NullReferenceExpection at "return tower == null && gameManager.Gold >= cost;" and at "gameManager.Gold -= tower.GetComponent().CurrentLevel.cost;". The game also doesn't take any money from purchases and you can spawn near infinite amounts of towers on one spot.

It worked all fine until I tried to add a menu that opens up and then you can choose a tower type you can buy.

So after I add all of this I get a NullReferenceExpection at return tower == null && gameManager.Gold >= cost; and at gameManager.Gold -= tower.GetComponent<TowerData>().CurrentLevel.cost;. The game also doesn't take any money from purchases and you can spawn near infinite amounts of towers on one spot.

Source Link

How can I get the assignment of an object back?

I have been struggling with this NullReferenceExpection error.

When I don't add anything and just use this version of the code. The game adds a tower, upgrades a tower and takes money for it.

What I changed in the current script is these. ---v It worked all fine until I tried to add a menu that opens up and then you can choose a tower type you can buy.

So after I add all of this I get a NullReferenceExpection at "return tower == null && gameManager.Gold >= cost;" and at "gameManager.Gold -= tower.GetComponent().CurrentLevel.cost;". The game also doesn't take any money from purchases and you can spawn near infinite amounts of towers on one spot.

//tower = (GameObject)

theTowerMenu.gameObject.SetActive (false);

transform.position = new Vector3(openSpot.transform.position.x, openSpot.transform.position.y, transform.position.z

If i take 'tower = (GameObject)' out of comments I get "error CS0030: Cannot convert type void' to UnityEngine.GameObject'".

The current script:

using UnityEngine;
using System.Collections;

public class PlaceTower : MonoBehaviour {

public GameObject towerPrefab;
private GameObject tower;

public GameObject openSpot;

private GameManagerBehavior gameManager;

public GameObject theTowerMenu;

// Use this for initialization
void Start () {
    gameManager = GameObject.Find ("GameManager").GetComponent<GameManagerBehavior>();
}

// Update is called once per frame
void Update () {

}

private bool canPlaceTower() {
    int cost = towerPrefab.GetComponent<TowerData> ().levels[0].cost;
    return tower == null && gameManager.Gold >= cost;
}

void OnMouseUp () {
    if (canPlaceTower ()) {
        //tower = (GameObject)
        theTowerMenu.gameObject.SetActive (false);
        transform.position = new Vector3(openSpot.transform.position.x, openSpot.transform.position.y, transform.position.z);
        Instantiate (towerPrefab, transform.position, Quaternion.identity);
        
        AudioSource audioSource = gameObject.GetComponent<AudioSource> ();
        audioSource.PlayOneShot (audioSource.clip);
        
        gameManager.Gold -= tower.GetComponent<TowerData>().CurrentLevel.cost;
    } else if (canUpgradeTower ()) {
        tower.GetComponent<TowerData> ().increaseLevel ();
        AudioSource audioSource = gameObject.GetComponent<AudioSource> ();
        audioSource.PlayOneShot (audioSource.clip);
        gameManager.Gold -= tower.GetComponent<TowerData> ().CurrentLevel.cost;
    }

}

private bool canUpgradeTower() {
    if (tower != null) {
        TowerData towerData = tower.GetComponent<TowerData> ();
        TowerLevel nextLevel = towerData.getNextLevel ();
        if (nextLevel != null) {
            return gameManager.Gold >= nextLevel.cost;
        }
    }
    return false;
}
}

Please help. I've been stuck with this for a long time.