Currency system (scripting): many problems

OK, first thing to point out is that there is one script that is coded in JS while the majority is in C#.

My game is 3D tower defense that in which you place towers that circular frequencies to disable on coming drones.

So, there is Game Object known as GameController and contains the following script (C#):
The main problem with this script is the fact that the variable (units) does not change when it is “told” to do so. I have also have two lines of code that sets the UI.text to the variables value. I had this in the OnGUI function but decided to put it back in the update function. So the main problem with this script is that the variable and UI.text do not change when the n key is pressed nor does the message to the console.

[code=CSharp]using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Units : MonoBehaviour
{


    public float units;
    public int unit_change = 10;
    public int Round_Number = 0; // not in affect yet. there is no round system
    public int int_ui;
    public UnityEngine.UI.Text UI_Units;

    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update() {
        int_ui = int.Parse(UI_Units.text); // THIS WAS IN UPDATE PUT I MOVED IT DOWN HERE AND NOTHING CHANGED
        UI_Units.text = units.ToString();
        unit_change = 10; // this is unnecessary as I have already set it in the initialization.

        if (Input.GetKeyDown(KeyCode.N)) // THIS IS NOT WORKING
        {
            print("n");                     // IT IS NOT PRINTING
            units += unit_change;           // THIS DOES NOT CHANGE FOR SOME REASON

        }





    }

    private void OnGUI()
    {
    }
}

[/code]

This script is the “placement block”.


It spawns the towers only if you have enough money. It will also charge you when you do place one. (this works which confuses me why the other does not). The script is full of unused code as it has been recycled from an older project of mine. This script is JS.

#pragma strict
var onhand : Transform;
public var GameController : GameObject;
var target : GameObject;
public var targeted : boolean = false;
var other : GameObject;
var NextNum : int = 0;
var RF_cost : int = 200;
var UI_Units : UI.Text;
var int_ui : int;
var str_ui : int;

function Update () {
    //print(GameController.GetComponent(Units).units);
    //print(UI_Units.text);
    this.transform.localEulerAngles =new Vector3(0,0,0);

    this.transform.position.x = Mathf.Round( onhand.position.x);
    if (Mathf.Round(onhand.position.y) >= 1){
        this.transform.position.y = Mathf.Round(onhand.position.y);
    }
    this.transform.position.z = Mathf.Round(onhand.position.z);

    GetComponent.<Rigidbody>().useGravity = false;

   // if(target == "Red"){

      //  GameObject.Find("menu_Blue").SetActive(false);
       // GameObject.Find("menu_Red").SetActive(true);

    }

    //if(target=="Blue"){
       // GameObject.Find("menu_Blue").SetActive(true);
        //GameObject.Find("menu_Red").SetActive(false);

   // if(Input.GetMouseButtonUp(0)){
  //
//   }

    if(targeted)
    {
        Debug.Log("2");
        if(Input.GetMouseButtonDown(1))
        {
            Debug.Log("3");
            Destroy(target);
        }
    }
    if(Input.GetMouseButtonDown(0)){
        if (!targeted){
            if (GameController.GetComponent(Units).units >= 200){
           // if (Units <= 200){
                var blockclone : GameObject;
                print("here");
                blockclone = Instantiate(target, Vector3(Mathf.Round(transform.position.x),Mathf.Round(transform.position.y),Mathf.Round(transform.position.z)), this.transform.rotation);
                print("here");
                blockclone.name = "Block"+NextNum;
                print("here");
                //Units = int(Units) - RF_cost;
                print("here");
 
                print(str_ui.ToString() + " UNITS");
                NextNum++;
            }
        }
    }
    //if (Input.GetMouseDown(0)){
    //    Instantiate(target, Vector3(Mathf.Round(transform.position.x),Mathf.Round(transform.position.y),Mathf.Round(transform.position.z)), this.transform.rotation);
   // }


function OnTriggerEnter ( other : Collider)
    {
    if(other.tag == "Block"){
        Debug.Log("1");
        if(Input.GetMouseButtonDown(1))
        {
            Debug.Log("2");
            Destroy(other);
        }
        target = other.gameObject;
        targeted = true;

    }
}

    function OnTriggerExit( other : Collider)
        {

            targeted = false;
        }

        function OnMouseDown () {
            if (!targeted){
                if (GameController.GetComponent(Units).units >= 200){
                    var blockclone : GameObject;
                    blockclone = Instantiate(target, Vector3(Mathf.Round(transform.position.x),Mathf.Round(transform.position.y),Mathf.Round(transform.position.z)), this.transform.rotation);
                    blockclone.name = "Block"+NextNum;
                    NextNum++;
                    GameController.GetComponent(Units).units -= RF_cost;
                    int_ui = int.Parse(UI_Units.text);
                    str_ui = int_ui - RF_cost;
                    UI_Units.text = str_ui.ToString();
                }
            }
        }

        function Blue(){
            target = GameObject.Find("Blue");
            GameObject.Find("menu_Blue").SetActive(true);  // this is not used
            GameObject.Find("menu_Red").SetActive(false);

        }
        function Red(){
            target = GameObject.Find("Red");
            GameObject.Find("menu_Blue").SetActive(false); // or this
            GameObject.Find("menu_Red").SetActive(true);

        }

I am also having problems will another C# script called “Drone_hieght” ( and yes i did spell height like that - don’t know how to change the name of a script :smile:). So this script has many functions but what i want it to do is when it hits the floor, I want it to add 10 units to the unit script. The error code sates: “object reference not set to an instance of an object”. Here it is the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Drone_hieght : MonoBehaviour
{
    //[System.NonSerialized]
    public float height;
    public float oldheight;
    public int repeats = 100;
    public int strength = 1;
    public float time = 6;
    public float pointb;
    public GameObject GameController;

    // Use this for initialization
    void Start()
    {
        height = Random.Range(5.0f, 7.0f);

    }

    // Update is called once per frame
    void Update()
    {
        transform.position = new Vector3(transform.position.x, height, transform.position.z);

        if (height <= 0)
        {
            //print("yo");
            //GameController.GetComponent<Units>().units += 10;
           // print(GameController.GetComponent<Units>().units);

            Destroy(gameObject);
            GameController.GetComponent<Units>().units += 10;
        }
    }

    void OnCollisionEnter(Collision collision)
    {



      //  if (collision.gameObject.tag == "Floor")
      //  {
            GameController.GetComponent<Units>().units += 10; // THIS DOES NOT WORK
            print(GameController.GetComponent<Units>().units);
            Destroy(gameObject);
      //  }
      //  else
      //  {

            Destroy(gameObject);
      //  }
    }

    void OnTriggerEnter(Collider col)
    {
        // if (found == false) {
        //cols = cols + 1;
        print("Detected collision between " + gameObject.name + " and " + col.GetComponent<Collider>().name);
        //print("some col");
        if (col.gameObject.tag == "RF")
        {
            print("SIGNAL");
            //founded = col;
            //Drone = GameObject.Find(col.transform.parent.gameObject.name).GetComponent<Drone_hieght>();
            //print(Drone);
            height -= 0.1f;
            Down();

        }
    }

    void Down()
    {
        oldheight = height;
        pointb = height - strength / 100;
        height = Mathf.Lerp(oldheight, pointb, time);




    }

}

Any help would greatly be appreciated, this is my first post and sorry for the very many unspecific questions.

I am completely open to questions about the code and I will try my best to answer them. I am quite new to scripting.

Thanks

Why do you use OnGUI ?

1 Like

Thanks for replying, I was looking through others threads and it was a common so I tried to do the same. The problem persisted and there was no noticeable change to any of the other processes. It was originally part of the update function.

You can build GUI without scripting, also put them inside prefabs and reinstantiate them when needed

1 Like