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 thenachotech1113 · Feb 01, 2014 at 06:01 AM · arrayschildminecraftorganize

set all the children of one object inside the same array

hello everyone, i am trying to make a space engineers like game, its supposed that you wiull be able to create your own vehicles, bases, weapos, etc. in a minecraft block placing sort of way. the script im having problems with is supposed to categorize the block that has been placed inside an array. that way i will be able to know how many engines will aplly thrust, how much will it weigh, and other sort of important stuff. i am trying to categorize them the following way:

 using UnityEngine;
 using System.Collections;
 
 public class Cube_manage : MonoBehaviour {
     
     public Transform selectedChild;
     
     public Transform[] chasiBlocks;
     public Transform[] gyroscopes;
     public Transform[] engines;
     
     void Update () {
         for (int i = 0; i < transform.childCount; i++){
             selectedChild = transform.GetChild(i);
                 if (selectedChild.tag == "Engine"){
                     selectedChild = engines[engines.Length ++];
                     break;
                 } 
                 
                 if (selectedChild.tag == "Chasi"){
                     selectedChild = chasiBlocks[chasiBlocks.Length ++];
                     break;
                 }
             
                 if (selectedChild.tag == "Gyro"){
                     selectedChild = gyroscopes[gyroscopes.Length ++];
                     break;
                 } 
         }
     }
 }

when a block is placed its automaticaly asigned to an empy object containing all the blocks of that spesific vehicle. all i have to do is to check the newly placed block's tag and that way it will put it in a array. i do not now how to make tis work, what yous ee above is the only thing that came to my mind. if you can give me any tips or ides on how to fix the code it will be much apreciated.

kind regards, Thenachotech

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
1
Best Answer

Answer by hellaandrew · Jun 03, 2014 at 07:54 AM

I believe that each array needs to have it's size updated on the fly. Your code doesn't know how big the array needs to be in order to hold the Transforms the user is adding. When the user adds a new Object (Transform), you need to update the size of the array. Also, with the way your code is currently typed out, it's going to be constantly increasing the size of the array, regardless of how many child objects it has.

Here is what I tried:

 using UnityEngine;
 using System.Collections;
 
 public class Cube_manage : MonoBehaviour {
     
     public Transform selectedChild;
     
     public Transform[] chasiBlocks;
     public Transform[] gyroscopes;
     public Transform[] engines;
 
     private int partsAmount = 0;
 
     void Update () {
         if (partsAmount < transform.childCount) {
             for (int i = 0; i < transform.childCount; i++) {
                 selectedChild = transform.GetChild(i);
                 if (selectedChild.tag == "Engine"){
                     engines = new Transform[engines.Length + 1];
                     engines[i] = selectedChild;
                     Debug.Log ("New Engines Length: " + engines.Length);
                     break;
                 }
             }
         }
         partsAmount = transform.childCount;
     }
 }

I tested my sample and it seems to be working out quite well. At the beginning of each Update frame, I check to see if the amount of children inside of the GameObject has changed since the last frame. If so, it will run the code to add the new Transform into the array. After doing so, it will store the amount of children it has into the partsAmount variable. If no new children are added, there's no reason to run that code.

I only used one of the arrays... but you should be able to copy it and apply it for the other arrays.

Good luck.

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.

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

C#: Arrays inside Arrays create a NullReferenceExeption? 1 Answer

Make a simple tree 1 Answer

Need help accessing children of an parent from a remote script. 1 Answer

Simple Tile Map 0 Answers

Type function(): Object[] does not support Slicing 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