- Home /
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
It spawns a set of GameObjects[] there's two of the same game object as a place holder.
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.
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
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!
Your answer
Follow this Question
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