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 Xalemich · May 29, 2014 at 10:05 AM · c#transformvariablearraylistvariable name

Variable values in Variable names

I am trying to fill an array with a large amount of Transforms from gameobjects in my scene. The purpose of this is to have a large amount of destinations I can LERP the main Camera to, in response to button press. What I am hoping would be possible is a more intelligent, and less manual, way of finding these Transforms, and adding them to the array. What I have now works, but is very tedious:

 using UnityEngine;
 using System.Collections;
 
 public class CommandBar : MonoBehaviour {
 
     public Transform pos1;
     public Transform pos2;
 
     private ArrayList posArrList = new ArrayList ();
 
     private int curr = 0;
 
     public Transform cam;
 
     private Rect lftRect = new Rect(Screen.width*1390/1600, Screen.height*785/900, Screen.width/16, Screen.height/9);
     private Rect rhtRect = new Rect(Screen.width*1490/1600, Screen.height*785/900, Screen.width/16, Screen.height/9);
 
     void Start () {
         posArrList.Add (pos1);
         posArrList.Add (pos2);
     }
 
     void OnGUI()
     {
         if ((Event.current.type == EventType.MouseUp) && lftRect.Contains (Event.current.mousePosition)) {
             curr--;
         }
         
         if ((Event.current.type == EventType.MouseUp) && rhtRect.Contains (Event.current.mousePosition)) {
             curr++;
         }
     }
 
     void Update () {
         //        
         MoveCam((Transform)posArrList[curr]);
     }
 
     void MoveCam (Transform camDest) {
         transform.position = Vector3.Lerp(transform.position, camDest.transform.position, Time.deltaTime * 3f);
     }
 }

Is it possible to at least add the many pos' (positions) to the array in a less "I'll just type everything" way, considering I will most likely be using 50+ possible destinations in the finished script?

Comment
Add comment · Show 2
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 fafase · May 29, 2014 at 10:06 AM 0
Share

DO you need them in a specific order or random is fine?

avatar image Xalemich · May 29, 2014 at 10:43 AM 0
Share

Yes, I need the camera to go from specific position to specific position.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Catlard · May 29, 2014 at 10:24 AM

Here's what I would do:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 
 public class TransformPositionGetter : MonoBehaviour {
     
     private List<Transform> _childTransforms;
     public Transform _parentToFindChildrenFrom;
     
     void Start () {
 
 
         _childTransforms = new List<Transform>();
         foreach(Transform child in _parentToFindChildrenFrom) {
             _childTransforms.Add(child);
         }
 
         //you could use _childTransforms.ToArray() here, if you want.
         //if you want to get a transform's position, just get it this way: childtransforms[3].position;
 
         _childTransforms = _childTransforms.OrderBy(x => x.name).ToList<Transform>();
         //this line uses linq to sort the list alphabetically, by name. If I were to translate this
         //line to english, it would read;
             //_childTransforms = itself, but ordering each x such that they are in order of x.name.
         //so, if you wanted to change the transforms to be ordered by their y position, you would instead
         //use this line of code:
         //_childTransforms = _childTransforms.OrderBy(x => x.position.y).ToList<Transform>();
 
     }
 }

Everybody happy?

Comment
Add comment · Show 3 · 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 Xalemich · May 29, 2014 at 10:46 AM 0
Share

I'll just juggle with it a bit, see if it does everything I hope it does, but thanks for the speedy answer, it seems like a very promising solution =D

avatar image Xalemich · May 29, 2014 at 11:00 AM 0
Share

Indeed, it works like a charm. I'd like to point out a little detail, just for future reference: The order of the children in the list is deter$$anonymous$$ed by the order in which they were placed under the parent object (or so it seems to me), and not the order in which they lie under it, or alphabetical order.

avatar image Catlard · Jun 05, 2014 at 08:15 AM 0
Share

Ah, well, in that case you'll probably want to sort them using linq. I'll update my original answer. Updated!

avatar image
0

Answer by Xalemich · May 29, 2014 at 11:21 AM

I'll juggle with it a bit, but it does look to be a promising solution =) Thanks a bunch for the speedy reply!

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

21 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 avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

How to sync a variable Transform via photon? 0 Answers

Converting this JS code to C# 1 Answer

Multiple Cars not working 1 Answer

Global Transform-one place to rule them all. 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