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 NewDeveloper. · Feb 25, 2014 at 03:21 PM · 2dgameobjectinstantiate

instantiate. Code Problem. Help.

Hello guys im trying to create my first game 2D but i face some troubles.. I have made 3 prefabs of 1)small ground 2)medium ground 3)big ground i have set my player to move to the right with some speed and the purpose of the game is to jump at these ground and try to make the best time without to fall out of them... like a jetpack..

now i want my smallground to instantiate it and as the player moves i want more grounds to generate automatically in random range.. so far i wrote this code.. but the problem with this code is that it generates only 1 small ground at random range and not lot of them .. my code:

 #pragma strict
 
 var smallG: Transform;
 
 function Start () {
       

var position: Vector3 = Vector3(Random.Range(-10.0,10.0),0,Random.Range(-10.0, ``10.0); Instantiate(smallG, position, Quaternion.identity); }

 function Update () {
 
 }


what i have to add so to create gorunds all the time? and second question i made the idea for this code from the scripting reference.. but i dont know why i put the code in the start method and not to the update method?

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 Dblfstr · Feb 25, 2014 at 03:41 PM

Start is called once, when the script is first activated. So you are only going to Instantiate one object. You need to have a spawn function that you call in Start, then have the spawn funciton call itself after a specified amount of time.

pragma strict

var smallG: Transform;

 function Start () {

     //Call the spawn function when the level is loaded.
     Spawn();
 }

 function Update () 
 {
 }

 //The spawn function will instantiate your object
 function Spawn()
 {
     var position: Vector3 = Vector3(Random.Range(-10.0,10.0),0,Random.Range(-10.0,10.0);
     Instantiate(smallG,position, Quaternion.identity); 

     Invoke("Spawn",5);

 }
Comment
Add comment · Show 9 · 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 NewDeveloper. · Feb 25, 2014 at 06:27 PM 0
Share

i checked lot times... but i get 2 errors.

1)Assets/Scripts/Instantiate!.js(25,20): BCE0070: Definition of 'Instantiate!.Spawn()' depends on 'Instantiate!.Spawn()' whose type could not be resolved because of a cycle. Explicitly declare the type of either one to break the cycle.

2)Assets/Scripts/Instantiate!.js(25,19): BCE0023: No appropriate version of 'UnityEngine.$$anonymous$$onoBehaviour.StartCoroutine' for the argument list '(function(): unknown)' was found.

avatar image perchik · Feb 25, 2014 at 06:32 PM 0
Share

That's because you have an infinite loop... You can't keep starting the same coroutine from inside that same coroutine

avatar image Dblfstr · Feb 25, 2014 at 07:43 PM 0
Share

Yes, sorry. That should be Invoke("Spawn",5);

avatar image Dblfstr · Feb 25, 2014 at 07:50 PM 0
Share

And the point of this is to create an infinite loop. One that will continue to spawn objects. You will need to stop this when your player dies, or when you reach the end of a level... if there is an end. This is for an infinite scenario, like an infinite runner.

avatar image NewDeveloper. · Feb 26, 2014 at 06:30 PM 0
Share

thats the name of my game.. but i have a problem again!

i enter this code:

 var position: Vector3 = Vector3(Random.Range(1.0,10.0),0,Random.Range(200.0,100.0));
       Instantiate(smallG,position, Quaternion.identity); 
  
     //wait 2 seconds
     yield WaitForSeconds(2);
  
     //Call the spawn function again
     StartCoroutine("Spawn",2);

i entered this script in other 2 prefabs.. at the first 5 metres all ok.. but after the 5 metres nothing appears no more grounds create... i have put range at x cordinates from 1.0 to 100.0.. so whats wrong? it should continue to create brackets "forever" untill 100.0 .. correct?

Show more comments

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

Multiple Cars not working 1 Answer

XBox Controller Angle of Fire Incorrect 0 Answers

How to Destroy the instances of 2 gameObjects at the same time? 1 Answer

instantiate gameobject with different size? 1 Answer

Player instantiates Backwords 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