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;
}

}

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# :slight_smile: If you don’t know either then go with C#

http://unity3d.com/learn

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