- Home /
Why am I getting this error ? - The type `RandomMovement' already contains a definition for `ChangeRotation'
The following is the code I'm using. I have no other script that has ChangeRotation as a variable that clashes with this one.
using UnityEngine;
using System.Collections;
public class RandomMovement : MonoBehaviour {
public float rotationSpeed;
public float movementSpeed;
public float ChangeRotation;
public float rotationTime;
void ChangeRotation()
{
if(Random.value > 0.5f)
{
rotationSpeed = -rotationSpeed;
}
Invoke(ChangeRotation,rotationTime);
}
void Start()
{
Invoke(ChangeRotation,rotationTime);
}
void Update() {
transform.Rotate (new Vector3 (0, 0, rotationSpeed * Time.deltaTime));
transform.position += transform.up*movementSpeed*Time.deltaTime;
}
}
Answer by Loius · Mar 31, 2014 at 06:54 PM
"float ChangeRotation"
"void ChangeRotation() {}"
You can't have a variable and a function that share a name.
On removing the float, I start getting errors in Unity. What do these errors mean ?
using UnityEngine;
using System.Collections;
public class Random$$anonymous$$ovement : $$anonymous$$onoBehaviour {
public float rotationSpeed;
public float movementSpeed;
public float rotationTime;
void Start()
{
Invoke(ChangeRotation,rotationTime);
}
void ChangeRotation()
{
if(Random.value > 0.5f)
{
rotationSpeed = -rotationSpeed;
}
Invoke(ChangeRotation,rotationTime);
}
void Update() {
transform.Rotate (new Vector3 (0, 0, rotationSpeed * Time.deltaTime));
transform.position += transform.up*movementSpeed*Time.deltaTime;
}
}
Assets/Scripts/Random$$anonymous$$ovement.cs(12,17): error CS1502: The best overloaded method match for
UnityEngine.$$anonymous$$onoBehaviour.Invoke(string, float)' has some invalid arguments - Assets/Scripts/Random$$anonymous$$ovement.cs(12,17): error CS1503: Argument
#1' cannot convertmethod group' expression to type
string'Assets/Scripts/Random$$anonymous$$ovement.cs(21,17): error CS1502: The best overloaded method match for
UnityEngine.$$anonymous$$onoBehaviour.Invoke(string, float)' has some invalid arguments - Assets/Scripts/Random$$anonymous$$ovement.cs(21,17): error CS1503: Argument
#1' cannot convertmethod group' expression to type
string'
$$anonymous$$anaged to fix it. I was not putting "ChangeRotation" in inverted commas during the invokes. Thanks for the help :)