- Home /
Find Transform with tag & add to array
Hey guys!
I am wondering how to find a "Transform with tag" via C# && add it to an Array of transforms.
using UnityEngine;
using System.Collections;
public class MSpawn : MonoBehaviour {
public GameObject playerPrefab;
public Transform [] spawners;
public int spawnTimer = 100;
public bool timerOff;
public void Start () {
// Search all spawn points
spawners = transform.Find ("Spawn Point");
// Start the player loading
//StartCoroutine(loadPlayer());
}
public void Update()
{
spawnTimer --;
if(spawnTimer <= 0)
{
spawnTimer = 0;
timerOff = true;
}
//Invoke("loadPlayer", 1);
StartCoroutine(loadPlayer());
}
// Load the player
private IEnumerator loadPlayer(){
// Search a spawn for the player (randomly)
int rand = Random.Range(0, spawners.Length);
Transform spawn = spawners[rand];
// Wait one second (the waiting time allows a better synchronisation on the players loading)
yield return new WaitForSeconds(1);
// Instantiate our player
Network.Instantiate(playerPrefab, spawn.transform.position, Quaternion.identity, 0);
}
}
This keeps giving me the error of, "Cannot convert Transform" to "Transform[]". How would i go about finding a gameobject in my scene via c#
I'll post my Javascript code as well to show you guys what DOES work, and what doesn't. I'm basically trying to convert this one function to C#.
//I want this replicated in C#---------------------------------------------
var target : GameObject;
var player : GameObject;
var timerOff : boolean;
var spawnTimer = 100;
function Update()
{
spawnTimer --;
if(spawnTimer <= 0)
{
spawnTimer = 0;
timerOff = true;
}
Invoke("selSpn", 0.1);
}
function selSpn()
{
var spawns = GameObject.FindGameObjectsWithTag("spawn");
target = spawns[Random.Range(0, spawns.length)];
spawnPlayer();
CancelInvoke("selSpn");
//I want this replicated in C#---------------------------------------------
}
function spawnPlayer()
{
Instantiate(player, target.transform.position, Quaternion.identity);
CancelInvoke("spawnPlayer");
}
Answer by robertbu · Jan 20, 2014 at 02:24 AM
You have three problems here. First, Transform.Find() does not return an array. It returns a singe Transform. Second, you don't declare space for the array, and third, the built-in arrays are fixed size, so you cannot add to them. I recommend you use a .NET generic list for this task. At the top of the file put:
using System.Collections.Generic;
Then to declare the list
public List<Transform> spawners = new List<Transform>();
Then to insert into the list:
spawners.Add( transform.Find ("Spawn Point"));
Note that 'Transform.Find()) only looks at first level children of the transform used as the base for the call. Is this what you are trying to do?
Answer by VildNinja · Jan 20, 2014 at 02:26 AM
First off: in js spawns is NOT an array of Transforms, it is an array of GameObjects
spawners = transform.Find ("Spawn Point");
transform.Find finds a single Transform with the given name in its own children. what you are looking for is to find an array of all GameObjects with the tag.
You should change spawneres to be of the type GameObject[] and then you can assing its value in the exact same way as it is done in js: spawners = GameObject.FindGameObjectsWithTag("spawn");
Answer by bakos · Jan 20, 2014 at 06:44 PM
Thank you guys! Honestly helped me out A LOT!! I decided to go with a gameobject search instead though, that way everyone's spawns are randomized, but i will be using this for object spawning! Thank you so much! Really streamlined the process for me!
Your answer
Follow this Question
Related Questions
Instantiate multiple objects from an array 1 Answer
Failed setting triangles in my mesh 1 Answer
Can you help me with arrays? 2 Answers
Need help with converting an array to a list in C#` 4 Answers