Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Kinziii · May 02, 2017 at 02:15 PM · arraydestroymathloops

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.textin 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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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);

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to convert and integer into an Array of his digits 4 Answers

Keeping object between edit and play modes. 1 Answer

Can't destroy an arrray with GUI Text 1 Answer

How to execute a function X times per second. 1 Answer

If I delete an object stored in an array, will it shorten the array or leave an index position with a null value? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges