Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 MilKID · Jan 30, 2017 at 07:35 AM · spawning problemsobject position

Endless Jumper Platform Spawning Script

I have made this script for infinite Jumper game and I want the platforms to spawn according to the camera height in random positions and to the get deactived again to reduce lag.

But I cant get the platforms to actually spawn. If anybody could maybe look over my script and tell me what i am doing wrong it would be very nice.

 ublic class Platform 
 {
     public GameObject platformObj;
     public string platformType;
     public Platform (GameObject obj, string t)
     {
         platformObj = obj;
         platformType = t;
     }
 }
 
 public class PlatformManagerScript : MonoBehaviour 
 {
     //PROPERTIES
     [Header("Platform Prefabs")]
     public GameObject[] platform;
     List<GameObject> platformBank = new List<GameObject>();
 
     [Header("Number of Platforms in Screen at once")]
     public int maxPlatforms = 100;
     public float spawnPlatformsTo;
     public float platformOffset;
     [Header("Screenlimitations")]
     public float screenBorderLeft;
     public float screenBorderRight;
 
     [HideInInspector]
     private float platformCheck;
     [HideInInspector]
     public Transform player;
     [HideInInspector]
     public float playerHeightY;
 
 
     void Start ()
     {
         player = GameObject.FindGameObjectWithTag("Player").transform;
 
 
 
         int numCopies = maxPlatforms * 3;
         for (int i = 0; i < numCopies; i++) 
         {
             for (int j = 0; j < platform.Length; j++) 
             {
                 GameObject o = (GameObject) Instantiate(platform[j], new Vector3(-20,-20,0),platform[j].transform.rotation);
                 o.SetActive(false);
                 platformBank.Add(o);
                 Debug.Log("Banked Platforms");
             }
         }
 
         ShuffleList();
 
     }
 
     void Update ()
     {
         playerHeightY = player.position.y;
         if (platformCheck < playerHeightY) 
             PlatformPositionManager ();
         UnactivatePlatform ();
 
         
     }
 
     void ShuffleList ()
     {
         System.Random rand = new System.Random ();
         int r = platformBank.Count;
         while (r > 1) 
         {
             r--;
             int n = rand.Next(r+1);
             GameObject val = platformBank[n];
             platformBank[n] = platformBank[r];
             platformBank[r] = val;
         }
     }
 
     void PlatformPositionManager ()
     {
         platformCheck = player.position.y + platformOffset;
         PlatformSpawner(platformCheck + platformOffset);
     }
 
     void PlatformSpawner (float floatValue)
     {
         float y = spawnPlatformsTo + 2.5f;
 
         while (playerHeightY > 0) {
 
             float x = Random.Range (-2.78f, 0.14f);
             Vector3 posXY = new Vector3 (x, y, 0);
             for (int n = 0; n < platformBank.Count; n++) {
                 GameObject o = platformBank [n];
                 if (!o.activeSelf) 
                 {
                     o.transform.position = posXY;
                     o.SetActive(true);
                     n = platformBank.Count + 1;
                 }
             }
 
             y += Random.Range(1f, 4f);
             Debug.Log ("Moved Platform");
         }
 
         spawnPlatformsTo = floatValue;
     }
 
     void UnactivatePlatform ()
     {
         foreach (GameObject platform in platformBank) {
             if (platform.transform.position.y < playerHeightY - 15) 
             {
                 platform.SetActive(false);
             }
         }
     }
 
 }
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

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Objects spawning in each other 1 Answer

im having problems spawning these prefabs in a grid next to each other with some spacing thanks in advance 0 Answers

Multiple spawn location with random game object in random time spawn c# 1 Answer

Scripts are being disabled during game play 0 Answers

Spawning a set amount of game objects that replaces the objects as they're destroyed 1 Answer


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