- Home /
Find the respawn objects and randomly spawn in one?
How could i do this because they are created randomly from another script, so I wont know how many there is and I wont be able to set it into the array variable in the script.
So i think i am going to have to use the gameObject.FindGameObjectWithTag("Respawn")?
How could i do this? I am stumped and confused
Answer by Alec-Slayden · Mar 27, 2011 at 03:27 AM
You should be able to add each object as they're created to an array, even if you don't know how many there will be.
Just add the object to the array right after its creation.
If you wanted to do the find method, take a look at GameObject.FindGameObjectsWithTag() make sure the "G" is capitalized for that. That will create an array of game objects that you can access.
EDIT:
If I understand correctly, the main problem you're having is that you can't assign an array to a prefab that doesn't yet exist in game.
What you can do is assign the list of respawn points to a static variable in a different script, and then reference that.
When you spawn your object, it doesn't have to be in the respawn point right away. You can instantiate it anywhere, perhaps out of view to be safe, and then let its script decide what to do next, E.G. randomly determine which respawn point to move itself to, referencing the static array of respawn points that was generated earlier.
The script that wants to respawn isnt activated till after.
You can still set variables in scripts that are not enabled. You can even use functions in them. There should be no problem modifying the array then using it when the script is enabled.
If for some reason you're not able to use your preferred script to store the variable, you can always store it in a different script to be referenced later, perhaps a good time to create a script for global variables?
ah I see, you have the script on the object to be spawned I take it?, editing the answer for this
Your answer
Follow this Question
Related Questions
Spawn object in random areas 2 Answers
Spawning - How to only have one object at a spawn at a time? 0 Answers
Creating a light in game C# 1 Answer
Randomly Initiate Cubes - Prevent Same Position 0 Answers
Random Position Script 1 Answer