Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 WillowFarmRacing · Jan 22, 2014 at 08:14 AM · item pickup

How to Fill Transform array using script ?

Hello! I have a prefab in Resources folder. This prefab has script which has the following line in script:

  var AllItems: Transform[];

In Hierarchy I have objects, like Item1, Item2, Item3... Now i have to manually drag the objects in to this prefabs script. But I need some kind of script to do this automaticaly for example at the Start() when the prefab is instantiated The script searches for i dont know - a word "Item" or what to add all objects with this name to the Transform[] array. How to script this ?

Thanks!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by GameVortex · Jan 22, 2014 at 08:53 AM

I would recommend using the function **FindGameObjectsWithTag** to find all the GameObjects **Tagged** with a specific tag and then fill your Transform array with the transforms of the found GameObjects.

Comment
Add comment · Show 4 · 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 WillowFarmRacing · Jan 22, 2014 at 09:58 AM 0
Share

Basicaly I did like this:

 var checkPointArray : Transform[];
 function Start () {
     
     checkPointArray = GameObject.FindGameObjectsWithTag("Checkpoints");

Got Error: BCE0022: Cannot convert 'UnityEngine.GameObject[]' to 'UnityEngine.Transform[]'.

Two questions, How to get in this case the Transform from GameObject? And second, How to correctly fill the array with found GameObject Transforms ?

Thanks!

avatar image GameVortex · Jan 22, 2014 at 12:29 PM 0
Share

To get the transform from the GameObject just access it using: gameObject.transform.

The rest is very basic array functionality. The function returns an array of GameObject. So first store the return in a temporary GameObject array.

Initialize your transform array with the same length as the GameOBject array.

Iterate through the GameObject array and place the transform of each GameObject into the same position of the transform array.

avatar image WillowFarmRacing · Jan 22, 2014 at 03:37 PM 0
Share

Function returns an array of GameObject, but these GameObjects are like not in right order(they are not in ascending order), can I do some kind of Sort ? I read that there is function Array.Sort(). But this function doesnt work for me, becouse I dont have defined Array by ...= new Array() , but ins$$anonymous$$d as var checkPointArray : GameObject[];

avatar image WillowFarmRacing · Jan 22, 2014 at 07:20 PM 0
Share

I have got the following code. I now find all GameObjects with tag "Checkpoints" and convert them to Array() in order to Sort them. Then I convert them back to Built in array. Now question , how to correctly copy or convert to other array which holds not GameObjects, but theyr transforms ? I kind a think about for loop at the bottom of script.

function Start () {

 var gos : GameObject[];
 var names = new Array(); 
 var dog = new Array();                               
 gos = GameObject.FindGameObjectsWithTag("Checkpoints"); 
   
 for (var go : GameObject in gos)  { 
     names.Push(go.name);
 }

 names.Sort();

 for (var name : String in names)  { 
     for (var go : GameObject in gos)  { 
         if(go.name==name){

             dog.Push(go);

         }
     }
 }

 return dog.ToBuiltin(GameObject);    


// Change GameObjects array to Gameobject.transform array for (var Checkpoints in gos) {

}

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.

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

Use editor icons in game? 2 Answers

How to respawn items along with player 1 Answer

Picking up flashlight? 0 Answers

Item collection in specific sequence. 2 Answers

pick item and add health script have problem 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