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 ELBANDID0 · Feb 27, 2013 at 01:19 PM · if-statementsonmousedown

.name = " " statement inside OnMouseDown.

Hi,

I would like to know (if possible) how to place various if statements inside OnMouseDown i.e.

OnMouseDown () {

if (gameObject.name="tube1") {

  //move upwards

}

if (gameObject.name="tube2") {

  //move downwards

}

}

Thanks in advance.

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
2

Answer by Dave-Carlile · Feb 27, 2013 at 01:20 PM

"=" is the assignment operator. "==" is the equality operator. So when comparing the name you need to use "=="...

 if (gameObject.name == "tube1")
 {
 }

Otherwise, not completely sure what you're asking for.

Comment
Add comment · Show 4 · 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 ELBANDID0 · Feb 27, 2013 at 04:00 PM 0
Share

Sorry that was a typo, I know the == sign but I can not assign separate objects within On$$anonymous$$ouseDown so within one function if I click on a blue box it moves it 10 up and if I click on a red box it moves it 10 to the right. i.e.

var BlueBox: GameObject;

var RedBox: GameObject;

On$$anonymous$$ouseDown () {

if (gameObject.name=="BlueBox") {

transform.position=Vector3(0,10,0); }

if (gameObject.name=="RedBox") {

transform.position=Vector3(10,0,0); }

}

avatar image ELBANDID0 · Feb 27, 2013 at 04:18 PM 0
Share

Sorry again as I realise that is also kind of misleading. That script would be attached to my main character and would be able to control the boxes from there.

Basically what I am trying to achieve is a player trying to climb a ladder so that the script is attached to my main character and when he clicks on the rungs of the ladder he lerps upwards. (used a shorter script above for ease of reading/typing quickly)

EDIT: and using "if" statement in this way does not achieve this.

+thanks for the help.

avatar image Dave-Carlile · Feb 27, 2013 at 04:24 PM 0
Share

Oh, I think I see. You want to do something different depending on which box is clicked? I would suggest adding a collider to the boxes, and in On$$anonymous$$ouseDown do a ray cast to deter$$anonymous$$e which box was clicked.

http://answers.unity3d.com/questions/13577/using-raycast-to-get-mouse-input.html

avatar image ELBANDID0 · Feb 27, 2013 at 04:47 PM 0
Share

Ah! Why did I not think of this and have wasted hours trying to do it the other way. I Should have been using raycasts from the beginning. Cheers Dave.

avatar image
1

Answer by Bunny83 · Feb 27, 2013 at 04:36 PM

In your case you should just make a public variable so you can assign the direction in the inspector:

 // UnityScript
 
 var direction : Vector3 = Vector3(10,0,0);
 
 function OnMouseDown ()
 {
     transform.position += direction;
 }

Just attach this to each of your boxes and set the direction the way you like in the inspector.

To be more flexible you could also add a Transform reference which will be moved. That way you can seperate the "trigger" from the affected object:

 // UnityScript
 
 var target : Transform;    
 var direction : Vector3 = Vector3(10,0,0);
 
 function OnMouseDown ()
 {
     if (target == null)
         target = transform; // if no target assigned, use the own transform
     target.position += direction;
 }
Comment
Add comment · Show 4 · 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 ELBANDID0 · Feb 27, 2013 at 04:50 PM 0
Share

Hey thanks for the answer, another answer suggested raycasts and I realised that's what I should have been thinking about from the beginning. Thank you though. $$anonymous$$ike.

avatar image Dave-Carlile · Feb 27, 2013 at 05:44 PM 0
Share

Actually, @Bunny83's answer is probably the better one. I didn't realize On$$anonymous$$ouseDown happened in response to clicking on a collider - thought it was just a normal "mouse was clicked" thing.

avatar image Bunny83 · Feb 27, 2013 at 10:03 PM 0
Share

@ELBANDID0: Well, the On$$anonymous$$ouseXXXX events are also using a raycast ;) Unity automatically casts a ray at the mouse position and sends messages to the collider it hits. Note that GUIElements, which can't be hit by a raycast since they are gui objects, also produce those events when clicked / hovered. Also watch out, in the past Unity didn't send these events on mobile devices. However it seems they changed this behaviour but i wouldn't blindly trust on that ;)

avatar image ELBANDID0 · Feb 28, 2013 at 10:34 AM 0
Share

Thanks for all the suggestions fellas. Currently I am sticking with the raycast method (probably because I am lazy and have already set it up) and it works fine for what I am trying to achieve. I will probably be implementing this method soon though.

Thanks for the help again.

avatar image
0

Answer by MickM · Feb 27, 2013 at 01:25 PM

Yes you can place multiple if statements in any function!

Also, and importantly, as above, you need to use == when comparing values You will get an error with that code and it won't compile because you are effectively assigning values instead of providing a condition check

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

12 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

Related Questions

OnMouseDown If-statement Question 3 Answers

change variable name inside IF statement, or add text to it 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


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