- Home /
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;
}
}
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.
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;
}
}
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.
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#
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!
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.