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 austinsun102 · Nov 22, 2018 at 04:46 AM · endless runnerobstacleendless

Spawn Different Obstacles for certain platforms

I am trying to create a game sort of like subway surfers and I want to Instantiate an obstacle every time a platform is Instantiated. There are going to be different types of platforms and different types of obstacles are going to spawn for each different platform. So, I created a gameobject array for Platforms and a gameobject array for the Obstacles. I don't know how I should go about this situation. I don't want to spawn an obstacle by using the start method from a script attached to each obstacle because I would need to use FindGameObjectsWithTag to get an obstacle array so I can instantiate an object because I'm pretty sure that FindGameObjectsWithTag uses quite a bit of cpu usage. Can you think of a way to insantiate an obstacle without using the start method?

 [SerializeField] private GameObject[] platforms;
 [SerializeField] private GameObject[] obstacles;
 [SerializeField] private Transform playerTrans;

 [SerializeField] private byte spawnAmount;

 private List<GameObject> activePlatforms = new List<GameObject>();

 private float spawnZ = 0f;
 private float tileLength;
 private int safeZone = 15;

 private void Start()
 {
     platforms = GameObject.FindGameObjectsWithTag("Platform Prefab");
     obstacles = GameObject.FindGameObjectsWithTag("Obstacle");

     tileLength = platforms[0].transform.localScale.z;
     Debug.Log(tileLength);

     for (int i = 0; i < spawnAmount; i++)
         _SpawnPlatform(0);

 }

 private void FixedUpdate()
 {
     if (playerTrans.position.z - safeZone > (spawnZ - spawnAmount * tileLength))
     {
         _SpawnPlatform(Random.Range(0, platforms.Length + 2));
         DestroyPlatform();
     }
 }

 private void _SpawnPlatform(int spawnIndex)
 {
     GameObject go = Instantiate(platforms[spawnIndex]) as GameObject;
     go.transform.position = Vector3.forward * spawnZ;
     go.transform.parent = transform;
     go.transform.rotation = Quaternion.Euler(Vector3.zero);
     
     spawnZ += tileLength;
     activePlatforms.Add(go);
 }

 private void DestroyPlatform()
 {
     Destroy(activePlatforms[0]);
     activePlatforms.RemoveAt(0);
 }

}

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
1

Answer by mlnczk · Nov 22, 2018 at 02:32 PM

Just create prefabs. Prefab containing whole surface and obstacles you want. Create as many as you want. Then put them all in array and randomize between them while spawning new random surfaces.

Comment
Add comment · 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

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

94 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I make Random Obstacle Spawn randomize between 4 or more prefabs? 2 Answers

so how can I place the randomly generated obstacles and coins along the up and down hill of the ground am driving on thank you 2 Answers

How to keep a gameobject on the ground ? 1 Answer

How can I destroy many instantiated objects on endless game 1 Answer

Trouble spawning endless platform 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