Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 faizanahmed089 · Apr 16 at 07:42 AM · spawn

Spawning from two different points with text

I want to spawn the obstacle exactly as per the below link.

https://www.youtube.com/watch?v=uQiwC06_cI8

I want exactly the same functionality of spawning obstacles with numbering. Two problems I faced: 1. I tried with two prefabs i.e. right/left with right Z position is 90 and left Z position is 60. But in this case in 02 seconds already 2 to 3 right obstacles shown on screen then left one will show 2. Unable to add increasing number functionality.

Anyone please guide me.

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 ogulcan_topsakal · Apr 17 at 05:15 PM

@faizanahmed089 You can set the correct increment values to the text by keeping the increment value in a variable and incrementing this value by one for each obstacle you spawn. I'm keeping my example very simple as I don't know at what level you are in software. Here is the example:

 [SerializeField]
 private GameObject obstaclePrefabRight;
 
 [SerializeField]
 private GameObject obstaclePrefabLeft;
 
 private int _spawnedObjectCount;
 public bool IsGameOver { get; set; } = false;
 
 public void StartSpawner(float initialDelay, float interval)
 {
     StartCoroutine(StartSpawnerEnum(initialDelay, interval));
 }
 
 private IEnumerator StartSpawnerEnum(float initialDelay, float interval, bool initialCall = true)
 {
     if (initialCall) yield return new WaitForSeconds(initialDelay);
     
     int positionRng = Random.Range(0, 1000);
     GameObject obstacle = Instantiate(positionRng % 2 == 0 ? obstaclePrefabRight : obstaclePrefabLeft, transform);
     obstacle.transform.localPosition = Vector3.zero;//SET POSITION ON ROAD
     _spawnedObjectCount++;
     
     if(obstacle.TryGetComponent(out TMP_Text tmpText))
     {
         tmpText.text = _spawnedObjectCount.ToString();
     }
 
     if (IsGameOver) yield break;
     yield return new WaitForSeconds(interval);
     StartCoroutine(StartSpawnerEnum(initialDelay, interval, false));
 }

If there are things you do not understand or want me to elaborate on, you can ask.

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 faizanahmed089 · Apr 18 at 11:47 AM 0
Share

Thanks a lot, Ogulcan. I just changed and add some things and now its working fine according to my requirements.

[SerializeField] private GameObject obstaclePrefabRight; [SerializeField] private GameObject obstaclePrefabLeft;

 private TextMesh rightText;
 private TextMesh leftText;

 public int textValueRight;
 public int textValueLeft;

 public bool IsGameOver { get; set; } = false;
 void Start()
 {
     textValueLeft = 2;
     textValueRight = 1;
     StartObstaclesSpawner(1, 1);
 }
 public void StartObstaclesSpawner(float initialDelay, float interval)
 {
     StartCoroutine(StartObsSpawnerEnum(initialDelay, interval));
 }

 IEnumerator StartObsSpawnerEnum(float initialDelay, float interval, bool initialCall = true)
 {
     if (initialCall)
     {
         yield return new WaitForSeconds(initialDelay);
     }
         
     Vector3 spawnPosRight = new Vector3(1.4f, 1.25f, 60f);
     rightText = obstaclePrefabRight.GetComponentInChildren<TextMesh>();
     rightText.text = textValueRight.ToString();
     textValueRight += 2;
     GameObject obstacleRight = Instantiate(obstaclePrefabRight, spawnPosRight, obstaclePrefabRight.transform.rotation);
     Debug.Log(textValueRight);

     Vector3 spawnPosLeft = new Vector3(-1.4f, 1.25f, 70f);
     leftText = obstaclePrefabLeft.GetComponentInChildren<TextMesh>();
     leftText.text = textValueLeft.ToString();
     textValueLeft += 2;
     GameObject obstacleLeft = Instantiate(obstaclePrefabLeft, spawnPosLeft, obstaclePrefabLeft.transform.rotation);
     Debug.Log(textValueLeft);

     if (IsGameOver) 
     {
         yield break;
     }
     yield return new WaitForSeconds(interval);
     StartCoroutine(StartObsSpawnerEnum(initialDelay, interval, false));
 }



avatar image faizanahmed089 · Apr 19 at 06:33 AM 0
Share

Please elaborate more step by step(if possible) as I am still facing the same problem of numbering. The obstacle spawning problem is solved but numbering on obstacles and increasing over each obstacle alternately on right-left is persist.

avatar image ogulcan_topsakal faizanahmed089 · Apr 19 at 09:30 AM 0
Share

@faizanahmed089 Can you briefly explain how you set the text of the objects or show some sample code?

ps. If you want to increase obstacle numbering on each spawn, increase _spawnedObjectCount after each Instantiate calls.

avatar image faizanahmed089 ogulcan_topsakal · Apr 20 at 10:13 AM 0
Share

Now it's done after some changes again.

avatar image
0

Answer by logicandchaos · Apr 17 at 05:28 PM

Increasing number functionality is really easy, just make an integer and when you spawn an obstacle you tick it up. Best to make it a static int in the script for the obsticle, then you can tick it up in the OnEnable method.

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 faizanahmed089 · Apr 18 at 11:48 AM 0
Share

Thanks a lot for your valuable response.

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

138 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 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

LAN playing game Controllers name + spawn point 1 Answer

How to change a model into an other model without clone-ing 1 Answer

script dosnt creat object where i want it to 1 Answer

Scene with few entrances/exits 2 Answers

Unity Engine issue... 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