• 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 bakos · Jan 20, 2014 at 02:05 AM · arrays

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");
 }
Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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?

Comment
Add comment · Share
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
1

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");

Comment
Add comment · Share
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
0

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!

Comment
Add comment · Share
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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

Getting C# to access a javascript global Array and Components 1 Answer

How would I call upon an asset in the projects tab with an array? 1 Answer

adding classes to arrays 2 Answers

Array of variable-length arrays (JS) 1 Answer

help understanding and manipulating arrays 1 Answer


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