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 crashoverride23 · Feb 13, 2014 at 12:22 PM · if-statementsonmousedownmultiple objects

OnMouseDown If-statement Question

Hi All, I am new to unity and having problem with the OnMouseDown-If script. I have 3 GameObject (cubes) that I want to return 3 different values. First Cube returns a value of 1, Second Cube returns a value of 2, and Third Cube returns a value of 3.

Here's what I have so far:

 public var cubeValue:int;
 var cube1 : GameObject;
 var cube2 : GameObject;
 var cube3 : GameObject;
 cube1 = GameObject.Find("Cube1");
 cube2 = GameObject.Find("Cube2");
 cube3 = GameObject.Find("Cube3");
 
 function OnMouseDown (){
     if (cube3)
         {
         cubeValue = 1;
         Debug.Log(cubeValue);
          }
      if (cube4)
         {
         cubeValue = 2;
         Debug.Log(cubeValue);
         }
     if (cube5)
         {
         cubeValue = 3;
         Debug.Log(cubeValue);
         }
 }


I would really appreciate if anyone can help me with this or atleast put me on the correct path. Thank you.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by getyour411 · Feb 15, 2014 at 02:41 AM

Edit: converted from comment to answer.

There is no IF statement in what I posted, not sure what you are referring to in your last comment.

Keep in mind that attaching the same script to different gameobjects does not mean they must have the same variable values. In other words, attaching your script to the three different cubes and change the public cubeVaue to 1, 2 and 3 - that's it, no if.

 public var cubeValue : int;
  
 function OnMouseDown() {
 Debug.Log(cubeValue)
 }
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
-1

Answer by Anusha · Feb 13, 2014 at 12:37 PM

you need to Raycast inside your OnMouseDown() function, then check which cube ....

 function OnMouseDown () { 

var hit: RaycastHit; var ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, hit)) {

 if (hit.collider != null)
  { 
 switch(hit.collider.name)
 {
  case "cube1":cubeValue = 1;
 Debug.Log(cubeValue);
 break;
 
 case "cube2":cubeValue = 2;
 Debug.Log(cubeValue);
 break;
 
 case "cube3":cubeValue =3;
 Debug.Log(cubeValue);
 break;
 
 
 }
 }
 
 }
 
Comment
Add comment · Show 5 · 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 crashoverride23 · Feb 14, 2014 at 10:37 PM 0
Share

Is it only possible to use Raycasting on this scenario? I'm new with Unity so I haven't learned yet on how to do raycast.

avatar image getyour411 · Feb 14, 2014 at 11:12 PM 0
Share

No, there are plenty of options. I'm not sure I agree with Anusha's response - what is that On$$anonymous$$ouseDown()ing? Are we saying that's attached to a Cube and we'll need to grow that mess with each new cube - no, that's not good.

avatar image crashoverride23 · Feb 15, 2014 at 01:07 AM 0
Share

Yes it's attached to a cube, and I only needed 3 cubes so I'm not growing it anymore. What are those other options getyour411?

Thanks for the response.

avatar image getyour411 · Feb 15, 2014 at 01:14 AM 0
Share

Your variable cubeValue is public, so in the Inspector set it to 1 for cube1, 2 for cube2, 3 for cube3

 function On$$anonymous$$ouseDown (){
 Debug.Log(cubeValue);
 }

Get rid of this

 var cube1 : GameObject;
 var cube2 : GameObject;
 var cube3 : GameObject;
 cube1 = GameObject.Find("Cube1");
 cube2 = GameObject.Find("Cube2");
 cube3 = GameObject.Find("Cube3");
 and anything related to Raycast.



avatar image crashoverride23 · Feb 15, 2014 at 02:03 AM 0
Share

$$anonymous$$y dilemma is actually on the if-statement, I can't seem to figure out how to get specific value on 3 different cubes when clicked.

avatar image
-1

Answer by erick_weil · Feb 15, 2014 at 02:42 PM

your " if (cube1)" verify not if tha current cube is the cube1, but if the cube1 exists. so,

if (cube1) = true, value =1

if (cube2) = true, value =2

if (cube3) = true, value =3

at the end will be debuged "1" "2" "3" all times, to debug only the cube wich is mouse over do this:

 public var cubeValue:int;
 var cube1 : String;
 var cube2 : String;
 var cube3 : String;
 cube1 = GameObject.Find("Cube1").name;
 cube2 = GameObject.Find("Cube2").name;
 cube3 = GameObject.Find("Cube3").name;
  
 function OnMouseDown (){
     if (gameObject.name == cube1)
        {
        cubeValue = 1;
        Debug.Log(cubeValue);
       }
     if (gameObject.name == cube2)
        {
        cubeValue = 2;
        Debug.Log(cubeValue);
        }
     if (gameObject.name == cube3)
        {
        cubeValue = 3;
        Debug.Log(cubeValue);
        }
 }


Comment
Add comment · Show 3 · 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 getyour411 · Feb 15, 2014 at 07:55 PM 0
Share

No - this is a repeat of what was posted above. Why are you putting all that junk in here, NONE of it is needed. He's declared public cubeValue. He attaches this script to three different cubes. He goes to each cube and in the inspector changes cubeValue to 1,2 3. THAT'S IT.

 public var cubeValue : int;
 
 function On$$anonymous$$ouseDown() {
 Debug.Log(cubeValue)
 }
avatar image erick_weil · Feb 15, 2014 at 07:59 PM 0
Share

ok, this works

avatar image getyour411 · Feb 15, 2014 at 08:03 PM 0
Share

The question is about as simple as it gets.

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

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

.name = " " statement inside OnMouseDown. 3 Answers

Object doesn't move 2 Answers

Tips on how to handle multiple if statemens 1 Answer

Disable Mouse Input when OnGUI() 1 Answer

How to read Shift + Click input? 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