Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Connermarbut · May 22, 2020 at 07:35 AM · prefabgameobjectsdisablescore systemspawnpoints

Spawning different GameObjects once player reaches certain score

I have a GameObject saved as a prefab that spawns random sets of platforms that go up the Y axis. and a Box Collider2D trigger that follows the player from behind that deletes the platforms and adds a point to a score system.


I would like to disable the prefab that spawns the current set of platforms at the start of the game, and enable a separate prefab that spawns a different pattern once the score reaches a certain number.


is there a way to do this?


  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class Spawner : MonoBehaviour
  {
      public GameObject[] platforms;
      
      public Transform spawner;
      private Spawner spawnerScript;
      private bool spawnPlatform = true;
  
      // Start is called before the first frame update
      void Start()
      {
          spawnerScript = GetComponent<Spawner>();
      }
  
      // Update is called once per frame
      void Update()
      {
          
      }
  
      void OnTriggerEnter2D(Collider2D col)
      {
          if (col.gameObject.tag == "Player")
          {
            if (spawnPlatform)
              {
              int rand = Random.Range(0, platforms.Length);
              Instantiate(platforms[rand], new Vector2 (-6.09f, spawner.position.y), Quaternion.identity);
                  spawnPlatform = false;
  
              }
  
         
          }
      }
  }

and heres the code that holds the scores and destroys the spawned platforms

  public int score;
  public Text scoreDisplay;
  public GameObject platform;
 
  // Start is called before the first frame update
  void Start()
  {
      platform = platform.GetComponent<GameObject>();
      
  }
 
  // Update is called once per frame
  void Update()
  {
      scoreDisplay.text = score.ToString();
 
  }
 
  void OnTriggerEnter2D(Collider2D col)
  {
      if (col.gameObject.tag == "ground")
      {
          StartCoroutine(Destroy());
          Destroy(platform);
      }
  }
 
  IEnumerator Destroy()
  {
      score += 1;
      yield return null;
  }


So the first script is attached to this

alt text

It spawns a set of GameObjects[] there's two of the same game object as a place holder.

alt text

alt textthe GameObject holds a set of patterns that spawn platforms. the "Spawn patterns" script is a single line of code that spawns the platforms. as you can see it also spawns a "spawner" so that it continuously spawns more as the player enters the trigger attached to it. the bool is there so it doesn't continue to spawn in the same place forever. Its kinda hard to explain but it works fine. I just want to adjust it so that I can spawn something else once the score reaches a certain number.

alt text

and the score script is tied to a trigger thats attached to the camera that follows the player. it deletes the platforms and also adds a single point to the score

Comment
Add comment · Show 1
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 jimm84 · May 22, 2020 at 09:58 AM 1
Share

Not being a guru in Csharp myself but a theory. Could you make it so that it either triggers and another script when "X" points is achieved or just add and 'if" stage and rinse repeat some of your code above to use the new gameOoject? basically duplicate your script in many ways and swap it for the new platform / gameObject?

" I just want to adjust it so that I can spawn something else once the score reaches a certain number."

eg -

 if (count >= 100) trigger "New Spawn"
 
     void New Spawn ()
     {              
             Debug.Log("New Spawn");
             (( instantiate ))
                        Destroy old platform
         }
     }

Just thoughts!

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

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

Disable all gameobjects from a prefab 2 Answers

How I can convert object to prefab instance? 2 Answers

Oh the HUMANITY!! How do I deactivate one game object and then activate another? 1 Answer

Prebuilt gameobject bundles without Prefab instancing behavior. 0 Answers

How do you spawn multiple prefabs within a certain distance from each other from one spawn point? 2 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