- Home /
How to destroy numbers after solving them?
I'm making a calculator game for my final project in a beginning scripting class. The premise of it is that randomly generated numbers are falling from the top of the scene, and when they hit a collider at the bottom, it plays a game over screen. You can only use a button once, and then you cannot click it again. If you use the calculator to come up with an equation that solves one of the falling numbers (for example, a 7 is falling. You use the calculator to type in 5+2 and the 7 will be solved), the number, which is a text UI, will be destroyed.
I have all this done, except for the part where the numbers are destroyed upon solving them. I've tried a simple if statement, where if the result.text (a variable that shows the product of the calculator input) equals the random.text, then the randomly generated number should be destroyed.
Below is my code, if needed to help:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class CalcScript : MonoBehaviour {
public InputField input1;
public InputField input2;
public Text result;
int number;
float quotient;
bool useInput2 = false;
int random;
public Text random1;
public int[] values;
void Start () {
random = Random.Range(1,10);
random1.text = random.ToString();
print(random);
}
public void Addition () {
gameObject.GetComponent<Button>().interactable = false;
number = int.Parse(input1.text) + int.Parse(input2.text);
print(number);
result.text = number.ToString();
}
public void Subtraction () {
gameObject.GetComponent<Button>().interactable = false;
number = int.Parse(input1.text) - int.Parse(input2.text);
print(number);
result.text = number.ToString();
}
public void Division () {
gameObject.GetComponent<Button>().interactable = false;
quotient = float.Parse(input1.text) / float.Parse(input2.text);
print(quotient);
result.text = quotient.ToString();
}
public void Multiplication () {
gameObject.GetComponent<Button>().interactable = false;
number = int.Parse(input1.text) * int.Parse(input2.text);
print(number);
result.text = number.ToString();
}
public void NumberButtons () {
gameObject.GetComponent<Button>().interactable = false;
Text number= gameObject.GetComponentInChildren<Text>();
print(number.text);
if (useInput2 == false)
{
input1.text += number.text;
}
else {
input2.text += number.text;
}}
void Update () {
if (Input.GetKeyDown(KeyCode.Tab))
{
useInput2 = true;
}
}
public void OnTriggerEnter (Collider random1) {
Application.LoadLevel("GameOver");
}
public void DestroyNumbers () { //This is where I'm having trouble. Logically, this should work, but no dice so far//
if (result.text == random1.text)
{
Destroy(random1);
}
}
}
What I would prefer to do is loop through the array list i made (public int[] values;), and if one of those values is equal to the result, it destroys the random number. My professor says he wants as short of code as possible, so I think a foreach loop or something like that would be my best bet, but all my attempts have given me nothing. They don't provide errors, but they just don't do anything either.
If you can help, I'd truly appreciate it! This has been driving me crazy, and I've tried to problem solve on my own, but it just hasn't yielded any results.
Answer by FM-Productions · May 04, 2017 at 11:09 AM
There are a few things I notice about your script, but the important part for your problem could be following: I do not see when DestroyNumbers() is called. You can either put it into the update function, so it gets called every frame, or call it if a user input occurs. The logic for checking string equally in DestroyNumber should work I guess.
Another thing is that in the function DestroyNumbers()
, the script could try to access random1.text
in the line if (result.text == random1.text)
after you destroyed it, which will most likely result in an error. So you may want to check if random1 is still ative before accessing text of random1.
Answer by toddisarockstar · May 03, 2017 at 04:07 AM
your problem is that the standard array you are using is not resizable. in the future i would use a list. https://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx
a list array has a built in function to remove an item.
but since you are using an array type that does not support removing items, Here is some code i wrote for you that will manually do it for you by using another temporary array. and fix your current problem. sorry to upset your proffessor but this is the shortest way i can think of to "loop through" manually without using a list.
this will remove a number from your values array how your code is setup.
int[] temparr;
int rid;
rid = 7;//<----- the number to remove from value array goes here
int idx;
int idx2 = 0;
idx = values.Length;
while(idx>0){idx--;
if(values[idx]==rid){idx2++;}}
print (" i found " + idx2 + " number " + rid+"'s");
temparr = new int[values.Length - idx2];
idx = values.Length;
idx2 = temparr.Length;
while(idx>0){idx--;
if(values[idx]!=rid){idx2--;
temparr[idx2]=values[idx];}}
print ("old array length:" + values.Length);
values = temparr;
print ("new array length:" + values.Length);