I need help with error in a C# script

Hi

I have trouble understaning why my script produces an error when I try to run it:

using UnityEngine;
using System.Collections;

public class RotatingCube : MonoBehaviour {
	
	public float cubeSpeed = 5.0f;
	
	
	
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		
		transform.Rotate(Vector3(0,1,0) * cubeSpeed * Time.deltaTime);
		
		//gameObject.transform.Rotate(0,1,0); 
		
		
	    
	}
}

The error(s) are:

Assets/Scripts/RotatingCube.cs(18,34): error CS0119: Expression denotes a type', where a variable’, value' or method group’ was expected

Assets/Scripts/RotatingCube.cs(18,27): error CS1502: The best overloaded method match for `UnityEngine.Transform.Rotate(UnityEngine.Vector3)’ has some invalid arguments

Assets/Scripts/RotatingCube.cs(18,27): error CS1503: Argument #1' cannot convert object’ expression to type `UnityEngine.Vector3’

The errors only occur when I use this line of code:

transform.Rotate(Vector3(0,1,0) * cubeSpeed * Time.deltaTime);

but not when I use this:

gameObject.transform.Rotate(0,1,0);

It is also worth to note that the script runs fine in the video tutorial I watched

Using Unity 3 (game programming) - Rotations and Translations 1

In C# you need to use ‘new’ when instantiating a object e.g.

transform.Rotate(new Vector3(0,1,0) * cubeSpeed * Time.deltaTime);

Hi

Thank you for helping out - adding the “new” did the trick !

However, reading through the examples in the Script Reference about Transform.Rotate they don’t seem to neither show or mention this, so I’ll guess that this information is just common knowledge.

Also, I’m a little puzzled why you’re saying instantiate as I’m not trying make a clone of an object, but merely trying to make an object rotate.

Anyway, now that it is working I’ll guess I shouldn’t worry to much.

You don’t instantiate your gameObject, you instantiate a Vector3 (which is also an object)

  • maybe this clarifies your question. :slight_smile:

Thank you for clarifying - I think I understand (I hope !)