• 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 kyrill91 · Jan 23, 2014 at 09:01 PM · prefabcollectible

Instantiate a prefab with random y position.

I have a camera following the player. Once the player reaches a specific point I instantiate a prefab just off screen. When that prefab passes through the scene, I recycle it and put it back to the front of the scene. Now I want to add a random range to the y axis, which works fine, except it only moves down, and it keeps moving down.

I have been staring at my script for hours and I just cant figure it out. Here's my code. Please excuse the organization, I know it's atrocious.

using UnityEngine; using System.Collections.Generic;

public class CollectibleScript : MonoBehaviour {

 public Transform prefab;
 public int numberOfObjects;
 public float recycleOffset;
 public Vector3 startPosition;
 public GameObject camera;

 private float x, y, z;
 private float resetX;
 private float resetY;
 private Vector3 nextPosition;
 private Queue<Transform> objectQueue;
 
 void Start () {
     x = PlayerController.distanceTraveled + 14f;
     y = camera.transform.position.y;
     z = -35f;
     resetX = PlayerController.distanceTraveled + 30f;
     resetY = y + Random.RandomRange (-7.5f, 5f);

     objectQueue = new Queue<Transform>(numberOfObjects);
     nextPosition = startPosition;
     for (int i = 0; i < numberOfObjects; i++) {
         Transform o = (Transform)Instantiate(prefab);
         o.localPosition = nextPosition;
         objectQueue.Enqueue(o);
     }
 }
 
 void Update () {
     startPosition = new Vector3 (x, resetY, z);
     if (objectQueue.Peek().localPosition.x + recycleOffset < PlayerController.distanceTraveled) {
         Transform o = objectQueue.Dequeue();
         o.localPosition = nextPosition;
         nextPosition.x += resetX;
         nextPosition.y += resetY;
         objectQueue.Enqueue(o);
     }
 }

}

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 nesis · Jan 23, 2014 at 10:16 PM

It doesn't look like you're changing resetY after Start(). Start is called only once in the lifetime of a MonoBehaviour, so to react to resetting your object, your should make a Reset() methods and call that in Start() and again when you want to reset the object.

Comment
Add comment · Show 1 · 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 kyrill91 · Jan 24, 2014 at 03:39 AM 0
Share

That's it! Thank you so much. I have no idea why I didn't try that.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

Tower defense target update 0 Answers

Problems with detecting tags 1 Answer

Swipe to place order in ShopScrollList (prefabs, object pooling, instantiating) 0 Answers

Draw prefab in OnPostRender (hud effect) 0 Answers

Apply Changes to Prefab - Unity 2018 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