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 13yearoldceo · Apr 13, 2014 at 07:22 PM · instantiateprefab2d-platformergenerated

my object keeps cloning is self i want it to clone once

hello there i am making a video game that require a obstacle generator similar to the one in flappy birds when the character goes though the trigger located in the center of the obstacles it is suppose to spawn another obstacle a few units up one at a time but it spawns a whole bunch in the air which slows down my computer can you guys help me make it spawn one at a time thx here is my script

pragma strict var max : float = 5; var min : float = 0; var obstacle : GameObject; var regenerate : boolean = false; var far : float = 0; var yes : boolean = true;

function Start () {

}

function Update () {

var x = Random.Range ( min, max);

var y = far + transform.position.y;

if(regenerate){

Instantiate( obstacle, Vector3(x,y,0),Quaternion.identity); regenerate = (false);

}

}

function OnTriggerEnter2D (other: Collider2D) { { regenerate = (true);}

}

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

2 Replies

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

Answer by akauper · Apr 13, 2014 at 08:10 PM

13yearoldceo... please use the code formatter button (the 010101 button up there) when submitting code. Your code is very hard to read as is.

Why are you instantiating the object within Update at all? If you only want ONE object to be instantiated per OnTriggerEnter2D, just put the code for the instantiation within that function. No need to bother with the regenerate boolean etc.

If for some reason that doesn't work for you, then you will need to track (within an array or list) the colliders which have entered your trigger. You will then need to check each entering collider against your list of colliders to make sure it does not already exist within the list.

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 13yearoldceo · Apr 13, 2014 at 10:40 PM 0
Share

Ok thx a lot I'll try

avatar image
0

Answer by sumeetkhobare · Apr 13, 2014 at 07:42 PM

I don't know why this happens, but i am currently making an endless runner too, and the same thing happened to me. The collider somehow produced multiple detections.

So i put a limit on the number of obstacles the scene can have at a time,including the ones not visible to the camera. Like:

 if(regenerate && numberOfObstacles < obstacleLimit){
 
 Instantiate( obstacle, Vector3(x,y,0),Quaternion.identity); regenerate = (false);
 numberOfObstacles ++;
 }

PS: you could either maintain the list of obstacles in an array and get the array count for the numberOfObstacles variable or you could go with this.but this has an overload of maintaining synchronization and also you have decrease it everytime you destroy an obstacle once it passes thru your scene.

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 13yearoldceo · Apr 13, 2014 at 10:42 PM 0
Share

Tats a good idea but not exactly wat I want though

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

22 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

Related Questions

Updating a variable on a script in an instanced object 1 Answer

Why is transform.position of a prefab that's been instantiated different than a gameobject created on the scene, when they are in the exact same position? 1 Answer

Instantiate object 1 Answer

Prefab instantiated wrong scale on mobile 0 Answers

autolink to prefab? 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