Reputation: 11
I am very new on developing games. I decided to make a game simply with a player and 5 different objects around it. Any time player collides with the one of the object, then all objects destroyed and respawn again. So far i managed to destroy them but couldt respawn them.
Could you please help me about it, Thank you :)
here is the code
public class collectibles : MonoBehaviour
{
public GameObject[] spawncollectable;
bool isobjdestroyed=false;
void Start()
{
float radians = 2 * Mathf.PI * 1.3f;
float vert = Mathf.Sin(radians);
float horiz = Mathf.Cos(radians);
var spawnDir = new Vector3(horiz + Random.Range(-0.1f, 1.3f), 0.2f, vert + Random.Range(-0.1f, -1.3f));
var spawnPos = transform.position + spawnDir;
Instantiate(spawncollectable[Random.Range(0, spawncollectable.Length)],spawnPos, Quaternion.identity);
}
private void Update()
{
if (isobjdestroyed == true)
{
float radians = 2 * Mathf.PI * 1.3f;
float vert = Mathf.Sin(radians);
float horiz = Mathf.Cos(radians);
var spawnDir = new Vector3(horiz + Random.Range(-0.1f, 1.3f), 0.2f, vert + Random.Range(-0.1f, -1.3f));
var spawnPos = transform.position + spawnDir;
Instantiate(spawncollectable[Random.Range(0, spawncollectable.Length)], spawnPos, Quaternion.identity);
}
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
GameObject[] obje = GameObject.FindGameObjectsWithTag("obj");
foreach (GameObject o in obje)
{
GameObject.Destroy(o);
isobjdestroyed = true;
}
}
}
}
Upvotes: 1
Views: 483
Reputation: 31
A solution: Rather than use GameObject.Destroy(), Use .SetActive(false) and .SetActive(true) when the object reaches destruction or any other state where you need it to change the objects state. Destroy is meant to remove the component from the scene completely (Great for bullets for example where you are instancing the object over and over and over). but you don't need to do that if you are respawning the object in a basic way. Just reset the the objects parameters such as health based on your requirements such as
if (health <= 0) // dont forget to reset your variables when you turn your object back on CharacterName.SetActive(fasle);
else CharacterName.SetActive(true);
There should be more elements to this but that is the general idea. you will need to add a respawn function, where it resets position, it will need to then reset its health by reassigning its value, and it will need (most likely) a call to your animation trigger. hope that helps. (this is how i would approach that problem. Though a basic explanation it should allow you to figure out your problem pretty quickly)
Also with your var variables that pertain to the vector 3, they are always going to be floats, doubles or greater. 3d space is vast and can be a big numbers some times requiring change from float to doubles or long doubles. But they should be declared as such rather than using "var" as the type. run a test each area in your game from edge to edge to determine the max distance you can travel and use the correct variable type. this will work out better long term when working on bigger projects. var should only be used when you dont know what the value will be but the areas in a game can be determined and set correctly with relative ease.
Upvotes: 1