Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 newto_unity · Apr 15, 2015 at 02:24 PM · raycasthitrandom.rangearray of gameobjects

onclick on random objects in gamemode and update score

Here I want to Choose a correct DYNAMIC RANDOM Object answer,compare two objects in monitor mouse position and select the correct object means update a score.am stuck with if ((hit.collider.gameObject).Equals(dub)) help me,

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

public class Rannum : MonoBehaviour { private TextMesh rnum; public TextMesh ans; public TextMesh anss; private TextMesh ansd1; private TextMesh ansd2; public GameObject[] aa; public GameObject dub; public RaycastHit hit;

 void Start () 
 
 {
             int n;
             n = Random.Range (1, 100);
             print (n);
             GameObject texq = GameObject.Find ("TextQ");
             rnum = texq.GetComponent<TextMesh> (); 
             rnum.text = "Number:" + n;

             int x = n;
             int y = (x / 3);
             int z = x - y;
             print (y);
             print (z);
             int n1 = (n - 1);
             int d1 = (n1 / 3);                                                
             int d2 = n1 - d1;
             int d3 = Random.Range (0, 50);
     
             int index = Random.Range (0, aa.Length);
             print ("index:"+index);
             //int a = 0;int b = 1;int c = 2;
     
             GameObject t1 = GameObject.Find ("Text1");
             GameObject t2 = GameObject.Find ("Text2");
             GameObject t3 = GameObject.Find ("Text3");
             
             
         
             if (index < 1) {
         
                     ansd1 = t2.GetComponent<TextMesh> (); 
                     ansd1.text = +y + "+" + d3;
         
                     ansd2 = t3 .GetComponent<TextMesh> (); 
                     ansd2.text = +d1 + "+" + d2;
             }
     
             if (index > 0 || index < 2) {
         
                     ansd1 = t1.GetComponent<TextMesh> (); 
                     ansd1.text = +y + "+" + d3;
         
                     ansd2 = t3.GetComponent<TextMesh> (); 
                     ansd2.text = +d1 + "+" + d2;
             }    
     
             if (index > 1) {
         
                     ansd1 = t1.GetComponent<TextMesh> (); 
                     ansd1.text = +y + "+" + d3;
         
                     ansd2 = t2.GetComponent<TextMesh> (); 
                     ansd2.text = +d1 + "+" + d2;
     }    
     dub = aa [index];
     ans = dub.GetComponent<TextMesh> (); 
     ans.text = +z + "+" + y;
     }
 void Update()
 {
     if (Input.GetMouseButtonDown (1)) {
                     Application.LoadLevel (0);
             }
             Ray ray = camera.ScreenPointToRay (Input.mousePosition);
             
         if (Input.GetMouseButtonDown (0)) 
             { 
                 if (Physics.Raycast (ray, out hit, 10))
                         {
                             if ((hit.collider.gameObject).Equals(dub))
                             
                             {    print ("write the code for score...!!!");}
                             else {  print("loose");    }
                 
                         }
             }
 }    

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by hbalint1 · Apr 15, 2015 at 03:44 PM

 if ((hit.collider.gameObject).Equals(dub))

You should never use this, unless you have overriden the Equals() method!

just use this instead:

 if (hit.collider.gameObject == dub)

this compares them by reference.

If you want some more complex comparing method, you must override Equals() and use it.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Generate random no and active that GO using array 1 Answer

Gameobject into arrays and choose someone in a random way, 1 Answer

Need help with lightning randomization 2 Answers

Randomizing a selection from an array 1 Answer

point of RaycastHit is changing, when ray is not. 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