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 CaptainIcy281 · Apr 21, 2012 at 09:54 PM · instantiategenerationendlesstunnel

Making an endless tunnel

Hi again!

Right now I'm trying to make an endless tunnel. I have say about 5 prefabs stuck together to make a tunnel at the beginning of the level, for a start. When you get close to the end of this pre-done set of tunnels, I want it to create say 5 or 10 more after those. (They're on the Z axis)

I've been messing around with the various Instantiate functions, and I know that's what I need to do but it's just not working the way I want it to.

I was using the first Instantiate script from Unity's Instantiate scripting page and it's almost exactly what I want. It creates them and I can specify how many, and how spaced apart they are on what axis. Got all that working, set up a timer to make it only instantiate 10 every 10 seconds, but the problem is, it's creating the same 10 over each other every time. It doesn't know that I want them created further along the Z axis, from the last batch. Is there an easy way to do this? Still not a whiz at scripting so any help would be appreciated. Thanks! :)

EDIT: My current script:

 var prefab : Transform;

 private var CreateTimer = 10.0;
 private var NextCreate = 0.0;
 
 function Update() 
 {
 
   if(Time.time > CreateTimer + NextCreate)
   {
     NextCreate = Time.time;
     for (var i : int = 0;i < 10; i++) 
     {
       Instantiate (prefab, Vector3(0, 0, i * 47.87), Quaternion.identity);
     }
 
   }
 
 }
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
1
Best Answer

Answer by ByteSheep · Apr 21, 2012 at 10:34 PM

If I understand correctly, then this should do the trick:

 var prefab : Transform;
 
 private var CreateTimer = 10.0;
 private var NextCreate = 0.0;
 private var count = 0;
 
 function Update() 
 {
 
   if(Time.time > CreateTimer + NextCreate)
   {
     NextCreate = Time.time;
 
     for (var i : int = 0;i < 10; i++) 
     {
 
       count++;
 
       Instantiate (prefab, Vector3(0, 0, count * 47.87), Quaternion.identity);
     }
 
   }
 
 }

Basically you need a variable that won't be reset each time the ten pieces have been created.. Hope this helps ;)

EDIT: Here's a slightly cleaner version using coroutines (untested).

 var prefab : GameObject;
 var segmentPrefabCount = 10;
 var pieceLength = 20.0f;
 var startPositionOffset = Vector3(0, 0, 0);
 private var pieceNumber = 0;
 
 
 function Start() 
 {
   // Create a new segment every 10 seconds
   InvokeRepeating ("CreateSegment", 10f, 10f);
 }
 
 
 function CreateSegment()
 {
   // Create a segment with segmentPrefabCount amount of prefabs
   for (var i = 0; i < segmentPrefabCount; i++)
   {
     CreatePiece();
   }
 }
 
 function CreatePiece()
 { 
   // Position for this piece will be the startPositionOffset plus the length of a prefab times it's index
   // Change Vector3.forward to whichever direction you need (e.g. -Vector3.up)
   var segmentPosition = startPositionOffset + Vector3.forward * (pieceNumber * pieceLength);
   Instantiate (prefab, segmentPosition, Quaternion.identity);
   pieceNumber++;
 }

Comment
Add comment · Show 4 · 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 CaptainIcy281 · Apr 21, 2012 at 10:55 PM 0
Share

That works perfectly! I knew it was simple, thank you so much. :)

avatar image Atrius · Apr 21, 2012 at 11:56 PM 0
Share

Glad to see you got it working.

avatar image SyabilNg · Dec 12, 2014 at 12:11 AM 0
Share

Hello,

I'm really new at scripting and to Unity itself. Doing some research to work on a game similar to this tunnel effect that looks like as if a character is falling through a hole.

I'd like to ask how does the NextCreate help with this situation because I'm stuck at understanding that part. And also why the value of 47.87?

Hopefully someone turns up to answer because this is an old thread. :)

avatar image ByteSheep · Dec 12, 2014 at 02:17 AM 0
Share

Edited my answer with a script that uses coroutines ins$$anonymous$$d of Time. Also added a couple comments to hopefully make it more clear what each line is doing.

avatar image
1

Answer by Atrius · Apr 21, 2012 at 10:08 PM

You need to instantiate the prefab as a GameObject and then translate its position to move it. Without seeing your existing script I can't help modify yours. Here's a C# script I just wrote up, there could be syntax issues I didn't actually compile this, but I hope it gives you an idea.

 public class TunnelGenerator : MonoBehavior {
     int segmentNumber = 0;
     float segmentLength = 15.0f;
 
     void CreateSegment() {
         // Increment what segment number you are on
         segmentNumber++;
         // Create a vector with your X,Y static and your Z the length of the prefab times the number of segments
         Vector3 pos = new Vector3(0.0f,0.0f,segmentLength * segmentNumber);
         // Instantiate it
         GameObject segment = (GameObject)Instantiate.(Resources.Load("TunnelPiece"));
         // Move it into place
         segment.transform.Translate(v, Space.World);
     }
 }
Comment
Add comment · Show 2 · 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 CaptainIcy281 · Apr 21, 2012 at 10:24 PM 0
Share

Yeah, I'm not too good with C#. I learned Java/UnityScript. That script makes some sense, though. And it does have some errors but I get the gist of things. Still not exactly sure how I should input my own values into that, though. I edited my question with the current script I was using if you want to take a look at it.

I'll figure it out eventually, so you don't have to stress yourself with this. :)

Also, I worked things out and the one error I get from your script is this: Assets/Scripts/InstantiateC.cs(14,26): error CS1061: Type UnityEngine.Transform' does not contain a definition for translate' and no extension method translate' of type UnityEngine.Transform' could be found (are you missing a using directive or an assembly reference?)

I would like to see how yours works ingame and I can probably convert it and tweak it.

avatar image Atrius · Apr 21, 2012 at 11:58 PM 0
Share

I believe the root cause of the issue is Translate is capitalized. I adapted this from memory off a system I had used but for $$anonymous$$e I was storing a reference to each piece. Your code is much cleaner assu$$anonymous$$g you don't need the reference and just need it loaded in the world.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Checking if object intersects? 1 Answer

Problem with Auto instantiating in X seconds in C# 1 Answer

Random instantiation endlessly? 1 Answer

My randomly generated slope game doesn't seem to work? 0 Answers

Platforms are been generated more and more to the left or right 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