{SOLVED, but not realy} working on a space shooter game and im having problems with my upgrade buttons.
so basically im trying to start off my upgrades system in my game. my first thought was to add an upgrade that can be bought so you can heal your self. so i made some code as seen below.
the problem is that it works if its blank but not once I put in the code to get .money and the .coin values (.coin is an audio source, while .money is an int) here is the code that works
void OnMouseDown() {
Debug.Log ("clicked");
//prints out that the item was clicked successfully! yay
}
but here is the code that doesnt work for some reason.
void OnMouseDown() {
Debug.Log ("clicked");
// noting is printed and noting happens. (the void() update loop does run though)
mon = ship.GetComponent<mover>().money;
coi = ship.GetComponent<mover> ().coin;
if (mon > 10) {
ship.GetComponent<mover> ().money = ship.GetComponent<mover> ().money - 10;
ship.GetComponent<mover> ().hp = ship.GetComponent<mover> ().hp + 1;
coi.Play ();
}
}
After alot testing it seems that setting the mon value and the coi value are the problem somehow. but it makes no sense. because even if they are messing it up then for 1, why is there no compiler errors. and for 2, why isnt the debug log being printed?
Answer by brendenderp · Jul 21, 2017 at 04:29 AM
further more trying to solve the issure i thought mabye its because im adding the health inside of onmouseDown? nope that wasnt it. so i tryed moving the code to void update... nope still noting.
here is what im at so far. (still non functional ) using System.Collections; using System.Collections.Generic; using UnityEngine;
public class buyHP : MonoBehaviour {
public int mon;
public GameObject ship;
public AudioSource coi;
public bool clk =false;
void OnMouseUp() {
// this is completely ignored by the compiler and unity. anything put in here is not ran.
Debug.Log ("clicked");
clk = true;
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//it prints this!
Debug.Log ("Running");
//this only gets ran if i change clk manualy in the inspector.
if (clk == true) {
mon = ship.GetComponent<mover> ().money;
coi = ship.GetComponent<mover> ().coin;
if (mon > 10) {
ship.GetComponent<mover> ().money = ship.GetComponent<mover> ().money - 10;
ship.GetComponent<mover> ().hp = ship.GetComponent<mover> ().hp + 1;
coi.Play ();
}
clk = false;
}
}
}
so now, since it seems as though there is no reason for this to be happening, i ended up fixing it. not by making my code better. but buy making it worse basically. i had to remove all the variables and ins$$anonymous$$d purely reference their counterpart in the mover script. so no it works. but its also ugly. here you go in case anyone needs this. (if any one from the unity higher ups is reading this PLEASE fix this issue there is no reason for the above code not to work compared to this one) ... NEVER $$anonymous$$IND .... this code actualy doesnt work. it worked once. i recompiled and now it doesn't???
public class buyHP : $$anonymous$$onoBehaviour {
public int mon;
public GameObject ship;
public bool clk =false;
void On$$anonymous$$ouseDown() {
// this is completely ignored by the compiler and unity. anything put in here is not ran.
clk = true;
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//it prints this!
Debug.Log ("Running");
//this only gets ran if i change clk manualy in the inspector.
if (clk == true) {
if (ship.GetComponent<mover> ().money > 10) {
ship.GetComponent<mover> ().money = ship.GetComponent<mover> ().money - 10;
ship.GetComponent<mover> ().hp = ship.GetComponent<mover> ().hp + 1;
ship.GetComponent<mover> ().coin.Play();
}
clk = false;
}
}
}