- Home /
Random movement script not working C#
This script doesn't work, I tried doing something relatively simple. I don't get compile errors, Unity just says
The associated script cannot be loaded Please fix any compile errors and assign a valid script
Here is my code, I'm trying to make an object that constantly spins and moves in random directions.
using UnityEngine;
using System.Collections;
public class Randommovement : MonoBehaviour {
void Update () {
int randomNumber = Random.Range(1,12);
if(randomNumber == 1)
{
transform.Rotate(1,0,0);
}
if(randomNumber == 2)
{
transform.Rotate(-1,0,0);
}
if(randomNumber == 3)
{
transform.Rotate(0,1,0);
}
if(randomNumber == 4)
{
transform.Rotate(0,-1,0);
}
if(randomNumber == 5)
{
transform.Rotate(0,0,1);
}
if(randomNumber == 6)
{
transform.Rotate(0,0,-1);
}
if(randomNumber == 7)
{
transform.Translate(1,0,0);
}
if(randomNumber == 8)
{
transform.Translate(-1,0,0);
}
if(randomNumber == 9)
{
transform.Translate(0,1,0);
}
if(randomNumber == 10)
{
transform.Translate(0,-1,0);
}
if(randomNumber == 11)
{
transform.Translate(0,0,1);
}
if(randomNumber == 12)
{
transform.Translate(0,0,-1);
}
}
}
I cannot test it at the moment, but you might be able to do this for your random rotation and translation:
transform.position += Random.insideUnitSphere * moveSpeed * Time.deltaTime;
transform.Rotate(Random.insideUnitSphere * rotationSpeed * Time.deltaTime);
Answer by Chronos-L · May 04, 2013 at 10:58 AM
Is the script named "Randommovement.cs"?
When you have this in the .cs:
public class AbcXyz : MonoBehaviour {
}
Then you should name the .cs as AbcXyz.cs.
Rename your file to match the classname or rename the class to match the file name. By the way, no space is allowed in the classname and the filename(I tried and it doesn't work). After rename, right-click->refresh (Ctrl-R) in the project tab just to make sure that the compilation is performed.
On a irrelevant matter, try to use if-else
whenever it is possible; and in this case, you can even use a switch-case
instead of a bunch of if
-statement.
As addition to this irrelevant but quite crucial matter, you should use the most elegant approach (I called it the lazy-fella way, because the most/more elegant approach typically has less code) to solve a problem (not brute force); .
/* --- Approach 1 ( Cleaner and easier to change )--- */
switch( randomNumber ) {
case 1:
transform.Rotate(1,0,0);
break;
case 2:
transform.Rotate(-1,0,0);
break;
...
}
If you are using the randomNumber
to do nothing more than just rotation, then go for this:
/* --- Approach 2 --- (The laziest way to do it)*/
Vector3[] rot;
void Start() {
rot = new Vector3[] {
new Vector3( 1, 0, 0),
new Vector3( -1, 0, 0),
...
};
}
void Update() {
transform.Rotate( rot[ Random.Range(0, rot.Length) ] );
}
Answer by RalphTrickey · May 05, 2013 at 01:32 AM
Try removing the space from the name. RandomMovement instead of Random movement.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
my random task generator not working 1 Answer
How to stop MoveTowards teleporting 1 Answer
Random Children Array that changes Parent, not working right 0 Answers