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 /
  • Help Room /
avatar image
0
Question by $$anonymous$$ · Nov 12, 2016 at 09:42 AM · c#2dcollision2ddestroygameobjectmaths

Applying a script to two objects not working

In short, I've got these blocks which, when collided with, generate a random + or - problem e.g 2 - 5. Now I originally only had one of these blocks in the first level but other levels will need more than one. I have been working for hours trying to find a way to allow blockade 1 and 2 to be able to be affected by the script. Once the question is answered correctly, the blockade will be destroyed allowing the player to continue. Here is the code:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 
 public class Collisions : MonoBehaviour
 {
     public GM gm = new GM();
     private int N1;
     private int N2;
     private int AnswerCorrect;
     public Text inputfield;
     public Text QuestionBox;
     public GameObject QuestionPanel;
     string opperand;
     public GameObject Blockade;
     public GameObject Blockade2;
     public GameObject Enemy;
     public KeyCode Submit;
 
     void Update()
     {
         if (Input.GetKey(Submit))
         {
             CheckAns();
         }
     }
     public void QuestionMaker()
     {
         string myOperator = GetRandomOperator();
 
         QuestionBox.text = "What is " + N1 + myOperator + N2 + "?";
     }
     public string GetRandomOperator()
     {
 
         int randomOperator = Random.Range(0, 2);
 
         N1 = Random.Range(1, 10);
         N2 = Random.Range(1, 10);
 
         switch (randomOperator)
         {
             case (0):
                 opperand = "-";
                 AnswerCorrect = N1 - N2;
                 break;
             case (1):
                 opperand = "+";
                 AnswerCorrect = N1 + N2;
                 break;
         }
         return opperand;
     }
 
     
     public void CheckAns()
     {
 
         if (inputfield.text == AnswerCorrect.ToString())
         {
             Destroy(Blockade);
             QuestionPanel.SetActive(false);
         }
         else
         {
             QuestionMaker();
         }
     }
     void OnCollisionEnter2D(Collision2D col)
     {
         if (col.gameObject.name == "End")
         {
             Scene scene = SceneManager.GetActiveScene();
             SceneManager.LoadScene(scene.buildIndex + 1);
         }
         if (col.gameObject.name == "Blockade")
         {
             QuestionPanel.SetActive(true);
             QuestionMaker();
             GetRandomOperator();
             CheckAns();
         }
         if (col.gameObject.name == "Blockade2")
         {
             QuestionPanel.SetActive(true);
             QuestionMaker();
             GetRandomOperator();
             CheckAns();
         }
     }
 }

I am well aware that this will not work because I removed all the things I tried so that whoever replies may have a crack at it. Thank you.

Comment
Add comment · Show 3
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 roman_sedition · Nov 12, 2016 at 02:18 PM 0
Share

CheckAns() should probably have a parameter for which gameobject to destroy.

i.e

CheckAns(Gameobject blockade)

Then it has a reference of which blockade to destroy when a question is answered correctly

  if (col.gameObject.name == "Blockade")
          {
              QuestionPanel.SetActive(true);
              Question$$anonymous$$aker();
              CheckAns(col.gameObject);
          }


Your Question$$anonymous$$aker is messed up, you should just cut

 N1 = Random.Range(1, 10);
 N2 = Random.Range(1, 10);

From GetRandomOperator and put it inside Question$$anonymous$$aker

You don't need to call GetRandomOperator directly after Question$$anonymous$$aker, as you already used GetRandomOperator to assign myOperator inside your Question$$anonymous$$aker

avatar image $$anonymous$$ roman_sedition · Nov 13, 2016 at 10:37 PM 0
Share

You can't put Gameobject blockade in the brackets

avatar image $$anonymous$$ $$anonymous$$ · Nov 13, 2016 at 10:38 PM 0
Share

Plus the issue is that both blocks will trigger a question but only one can be destroyed. The same block is always destroyed no matter what block the question is answered on.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by roman_sedition · Nov 15, 2016 at 04:47 AM

You need to put it in the function declaration of CheckAns, that way you can use a GameObject parameter for CheckAns()

 public void CheckAns(GameObject blockade)
 {
               
 //your block of code
 
 }


This way you pass whichever blockade is asking the question as an argument, if the answer is right it destroys the blockade which was passed as a GameObject Reference

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Is calling OnCollisionEnter2D on allot of GameObjects leading to worse performance? 0 Answers

Can't make the player attack an enemy 0 Answers

Problems with raycasting 2D. Probelmas com raycasting 2D. 0 Answers

Checking if the player jumps while not grounded 0 Answers

How do I move a game object only on the x axis with arrow keys? 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