• 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 Xolve · Sep 06, 2015 at 02:20 PM · c#instantiaterandom.range

Instaniate not working with Random.Range

I'm making a game in which you tap to jump over obstacles.
I've got everything working except the randomizing of the distance between one obstacle and the next.
The objects are not being created at a far enough distance away from each other for them to be jumpable, Why won't this work:

 void CreateOb() {
        lastPos = 21f;
         Vector3 cdsa = new Vector3(Random.Range(4 + lastPos, 16 + lastPos) , -0.53f, -5.42f);
         GameObject ObstacleInstance;
         ObstacleInstance = Instantiate (Obs, cdsa, rot.rotation) as GameObject;
         lastPos = ObstacleInstance.transform.position.x;

I am using InvokeRepeating() in the Start() method to repeat this.
The objects just seem to stack up beside each other instead of being further away...
Please Help :)

Comment
Add comment · Show 1
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 Xolve · Sep 06, 2015 at 02:23 PM 0
Share

using this in start():
InvokeRepeating ("CreateOb", 2f, 2f);

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by hexagonius · Sep 07, 2015 at 09:19 PM

pull this line out of the code block to somewhere earlier and you're good to go:

 lastPos = 21f;
Comment
Add comment · Show 5 · 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 Xolve · Sep 08, 2015 at 10:19 PM 0
Share

I haven't got time right now to test this, but will in about 15 hours so will update then, thanks!!!

avatar image Xolve · Sep 10, 2015 at 07:55 PM 0
Share

Hmm... there still seems to be an issue @hexagonius, they are spawning better but are still spawning behind the lastPos?? trying to see why now

avatar image hexagonius · Sep 11, 2015 at 05:01 AM 0
Share

Isn't that what you wanted? lastPos is an ever increasing value if I'm not mistaken.

avatar image Xolve · Sep 11, 2015 at 10:23 PM 0
Share

@hexagonius $$anonymous$$inda, I'm trying to get the objects to instaniate in front of (so object A at x 21, then next at say x 31) I can't figure out why. I think it is unable to get the X transform from the instaniated object - working at it now

avatar image hexagonius · Sep 12, 2015 at 08:08 AM 0
Share
 Vector3 lastPos = new Vector3{21, -.53f, -5.42f};
 void CreateOb() {
 Vector3 cdsa = lastPos + Vector3.right * Random.Range(4 + lastPos, 16 + lastPos);
 GameObject ObstacleInstance;
 ObstacleInstance = Instantiate (Obs, cdsa, rot.rotation) as GameObject; lastPos = ObstacleInstance.transform.position;

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

29 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 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 avatar image

Related Questions

local scale not working? 2 Answers

Random.range game object destroying itself before reaching destroy position.Please help. 1 Answer

Instantiating random prefabs gives wrong position 3 Answers

*Solved* Loop instantiating objects causes my Unity Editor to crash 1 Answer

Instantiate particle system attached to object 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges