Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Steeley · May 04, 2013 at 10:25 AM · c#random

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);
         }
     }
 }
 
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Steeley · May 04, 2013 at 05:27 PM 0
Share

Almost, its called Random movement

avatar image robertbu · May 05, 2013 at 02:02 AM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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) ] );
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by RalphTrickey · May 05, 2013 at 01:32 AM

Try removing the space from the name. RandomMovement instead of Random movement.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges