Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Caleb_1701 · Dec 21, 2015 at 04:26 PM · randomlevel design

Random Level Generation, unknown problem

Can someone tell me what's wrong with this code? I checked the position of the controller object that the script is attached to and it looks like it didn't move.

  using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class randomlevel : MonoBehaviour {
     public int randomness;
     public int lvl_size;
     private Vector3[] ways;
     void start () {
         Vector3[] ways = new Vector3[4] {new Vector3(0,0,5), new Vector3(0,0,-5), new Vector3(5,0,0), new Vector3(-5,0,0)};
         int thatway = 0;
         List<Vector3> places = new List<Vector3> ();
         places.Add (transform.position);
         for (int i = 0; i <= lvl_size; i++) {
             transform.position = new Vector3 (transform.position.x + ways[thatway].x, 0, transform.position.z + ways[thatway].z);
             places.Add (transform.position);
             print("It works");
         }
     }
 }

Note, the randomness property is not yet used, but once I get this problem solved I will add that part in. At this point the position of the controller object should end up at (0,0,25) because I set lvl_size to 5.

Any help would be appreciated.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by wibble82 · Dec 21, 2015 at 05:21 PM

Hi

I'm not sure exactly what that code is supposed to do - you didn't mention that in your question! :)

There is an obvious issue though - you have called your function 'start', not 'Start'. C# is case sensitive, so your function won't be getting called at all automatically. If you're relying on that, then none of your code will be getting called.

I'm not sure where the 'print' function came from either?

After that, the code doesn't look like it will actually do a great deal of work - it is effectively just fulling in a list of vector3s by changing the transform of random level repeatedly. Maybe what you're looking for is something more along the lines of:

         public class randomlevel : MonoBehaviour
         {
             public int randomness;
             public int lvl_size;
             
             //private Vector3[] ways; //this is redundant as we define a list of ways inside the start function!
 
             // Start function gets called when randomlevel is initialized (note capital S!!)
             void Start()
             {
                 //our potential ways
                 Vector3[] ways = new Vector3[4] { new Vector3(0, 0, 5), new Vector3(0, 0, -5), new Vector3(5, 0, 0), new Vector3(-5, 0, 0) };
 
                 //our list of places
                 List<Vector3> places = new List<Vector3>();
 
                 //starting place is the transform of the randomlevel object?
                 Vector3 current_place = transform.position;
 
                 //add the first place
                 places.Add(current_place);
 
                 //add a set of extra places along a randomly generated path
                 for (int i = 0; i <= lvl_size; i++)
                 {
                     int thatway = 0; //replace 0 with random number between 0 and ways.Length (see Random.Range)
 
                     //add the 'way' vector to the current place to get the next position
                     current_place += ways[thatway];
 
                     //store current place in 
                     places.Add(current_place);
 
                     //print something nice!
                     Debug.Log("Added place " + current_place.ToString());
                 }
 
                 //presumably now we'd do something with our places list?
             }
         }

That has a few floors, but might get you started :)

Comment
Add comment · Show 3 · 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 Caleb_1701 · Dec 21, 2015 at 09:53 PM 0
Share

The "print" function just displays the text on the console. I'm not sure what the difference is between that and "Debug.Log" (I thought Debug.Log was just in Javascript, but it appears I'm wrong about that). And as for the script not really doing anything, that's because I couldn't actually write the more complicated code until the error was fixed. I'm not sure how I let such a simple mistake get past, but thanks! And I think I can figure out the "random" part myself, but if I need some help it will be nice to have the answer right here already.

avatar image wibble82 Caleb_1701 · Dec 22, 2015 at 09:58 AM 0
Share

No problem :) Do you want to mark it as 'right' then - just so others with similar questions or people looking for questions that need answering don't get confused!

avatar image Caleb_1701 wibble82 · Mar 29, 2016 at 11:59 PM 0
Share

Sorry it took me so long to mark your answer as "correct".

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Random Number every 5 seconds 2 Answers

Help with Randomized Sprite on GUI button click 0 Answers

How to make an enemy move randomly in the environment in Unity5? 0 Answers

Printing a random value from an array. 1 Answer

Random Scenes Without Repetition 1 Answer


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