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 Stugger · Nov 28, 2014 at 08:41 PM · renderercubeonmousedownonmouseoverchange color

Need help with my first script, changing cube colors

Hey so I only barely downloaded Unity a week ago or so and I'm starting to get in to the coding aspect of it where I am a complete beginner, I've read a book on java a long time ago which doesn't really help considering I read it so long ago, anyway, can anybody tell me whats wrong with my script? The debug.log works when I'm clicking but the cube doesn't change colors and I'm getting no compiler errors. This is my first script and I decided to mess with the OnMouse functions, so any help directed towards a beginner would be much appreciated.

 using UnityEngine;
 
 using System.Collections;
 public class Block_Start : MonoBehaviour {

 void OnMouseDown () 
 {
     Debug.Log("Mouse is Down");
     renderer.material.color = Color.black;

}

 void OnMouseUp() 
 {
         Debug.Log("Mouse is Up");
         renderer.material.color = Color.red;
     }

 void OnMouseOver () 
 {
     
     if (Input.GetMouseButtonDown(1))
         Debug.Log("Secondary Mouse is Down");
         renderer.material.color = Color.green;

     if (Input.GetMouseButtonUp(1))
         Debug.Log ("Secondary Mouse is Up");
         renderer.material.color = Color.blue;

     if (Input.GetMouseButtonDown(2))
         Debug.Log ("Back to Default");
         renderer.material.color = Color.white;
 }
     

}

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 Graham-Dunnett ♦♦ · Nov 28, 2014 at 08:42 PM 0
Share

On$$anonymous$$ouseOver() means the mouse is hovering over the object. You're checking to see if the mouse button is pressed, which it might not be.

avatar image Stugger · Nov 28, 2014 at 09:46 PM 0
Share

Ah that makes sense, well now I did this and the first two work now, the cube goes blue and black, but apparently the compiler is telling me that I cant reuse On$$anonymous$$ouseDown and On$$anonymous$$ouseUp... any ideas? I'm thinking maybe $$anonymous$$onoBehaviour may be a reason cosidering I'm trying to get it to do more than one thing?

 using UnityEngine;
 using System.Collections;
 
 public class Block_Start : $$anonymous$$onoBehaviour {
 
     void On$$anonymous$$ouseDown () 
     {
         Debug.Log("$$anonymous$$ouse is Down");
         renderer.material.color = Color.black;
 }
         
     void On$$anonymous$$ouseUp() 
     {
             Debug.Log("$$anonymous$$ouse is Up");
             renderer.material.color = Color.red;
         }
 
     void On$$anonymous$$ouseDown () 
     {
         
                 if (Input.Get$$anonymous$$ouseButtonDown (1))
                         Debug.Log ("Secondary $$anonymous$$ouse is Down");
                 renderer.material.color = Color.green;
 }
 
     void On$$anonymous$$ouseUp()
     {
                 if (Input.Get$$anonymous$$ouseButtonUp (1))
                         Debug.Log ("Secondary $$anonymous$$ouse is Up");
                 renderer.material.color = Color.blue;
 }
 
     void On$$anonymous$$ouseDown()
     {
                 if (Input.Get$$anonymous$$ouseButtonDown (2))
                         Debug.Log ("Back to Default");
                 renderer.material.color = Color.white;
     }
 
 }
avatar image jackhearts · Nov 28, 2014 at 10:35 PM 0
Share

Your original thinking was sound enough, see my answer. You can't reuse the same name for a method unless you overload it. The On$$anonymous$$ouseUp/Down/etc methods are basically the same as any other method (function) you might write. Except in this case unity will invoke it if a particular mouse event occurs.

$$anonymous$$onoBehaviour is simply the base class that all unity scripts derive from. In C# a colon is used to show that a particular class derives (inherits) from another. You can take away $$anonymous$$onoBehaviour but none of the unity methods or properties will be available.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by jackhearts · Nov 28, 2014 at 10:06 PM

You're missing the braces from the IF statements, the block of IF's should look like this.

         if (Input.GetMouseButtonDown(1))
         {
             Debug.Log("Secondary Mouse is Down");
             renderer.material.color = Color.green;
         }
         if (Input.GetMouseButtonUp(1))
         {
             Debug.Log ("Secondary Mouse is Up");
             renderer.material.color = Color.blue;
         }
         if (Input.GetMouseButtonDown(2))
         {
             Debug.Log ("Back to Default");
             renderer.material.color = Color.white;
         }

An IF statement without braces will only execute what comes after it up to the first line ending semi-colon. What's happening in your code is that the if statement is firing and sending the debug message but ignores the color change as it is after the semi-colon. Personally I don't like that C# allows if statements without braces.

Have you had a look at the links below, can help you with the basics of unity. I'd recommend running through some tutorials on youtube as well or maybe buy a book. You'll want to learn about the language itself, whether that's C# or unity's version of javascript. That java book you read through probably won't be much use since the above is C# :) If you don't know either then go with C#

http://unity3d.com/learn

http://docs.unity3d.com/ScriptReference/

Comment
Add comment · Show 2 · 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 Stugger · Nov 28, 2014 at 10:51 PM 0
Share

Awesome, thank you that helped a lot, and yeah I know its C# which I've heard isn't too far different from Java? I've also been cruising the 3d tutorials with C# selected as my default language. Once again, thank you!

avatar image jackhearts · Nov 28, 2014 at 11:41 PM 0
Share

no problem. It kinda has similar syntax, I think C# goes about things differently and can do more although I'm not too familiar with java to really know. Having a firm understanding of program$$anonymous$$g is more important than the language. Once you've got that it's much easier to look at different languages and see what's going on. The good thing about C# and Unity is that it usually points you straight at any problems.

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

OnMouseDown for right mouse click without being hacky? 1 Answer

Need material.color to revert. 3 Answers

How can I call OnMouseDown() from an outside script? 1 Answer

Accessing GameObjects Color To Store In Variable 1 Answer

Change Object Color Using Tags OnMouseOver 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