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 d112570 · Sep 23, 2013 at 05:30 PM · gameobjectvariablestring

GameObject to Variable to GameObject and add string?

 private GameObject planet;

Ok I turned this gameObject into a variable called "planet".

 planet = GameObject.Find(SystemSizeSetup[i].name);

Then I want to test to see if it worked and it worked.

   print (planet.transform.localScale);

but adding a string to it, does not work.

 print ((planet + "Cloud").transform.localScale);

I have cloud, lava, lightning that I would like to add to each planet object, how would you go about doing that? I need to find the shortcuts so its easier to read the scripts.

Having a code like this is easy to read and search for errors.

 if (Satellite) Satellite.transform.RotateAround(Planet.transform.localPosition, Satellite.transform.up,  SatelliteSpeed);
 
 
Comment
Add comment · Show 5
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 clunk47 · Sep 23, 2013 at 05:56 PM 1
Share

You need to specify what you mean "It does not work". Are you getting an error? Please be more specific on what you are trying to accomplish.

avatar image d112570 · Sep 23, 2013 at 06:13 PM 0
Share

Sorry, it gives an error, says string does not contain a definition. Ins$$anonymous$$d of the whole time typing in.

     GameObject.Find(SystemSizeSetup[i].name + "Cloud").transform.localScale;
 I like the shortcut version of

 (planet + "Cloud").transform.localScale

It's a lot easier to read, I have like planet + cloud1, planet + cloud2, planet + lava1 etc.

avatar image robertbu · Sep 23, 2013 at 06:16 PM 1
Share

I can clearly see why things are not working, but I don't understand what you are trying to do. Are 'cloud', 'lava' and 'lighting' other game objects or are they scripts? Are they children of 'planet'?

avatar image d112570 · Sep 23, 2013 at 06:24 PM 0
Share

I have different objects for example; Parent object is for example Venus and her children are Cloud1, Cloud2, Lightning1, Lightning2 etc. So if finds Venus + Cloud1, another object would be Venus + Lightning2.

avatar image robertbu · Sep 23, 2013 at 07:10 PM 1
Share

I'm still unclear about the structure of your project and your goal here. There are several ways to deal with children. Find() will only find the first level children unless you specify a path. You can create a list of all children at whatever depth using:

 var transforms = gameObject.GetComponentsInChildren(Transform);

Also there is source code in the Transform reference page that walks the first level of children. Note that the name must match exactly including the case of letters and any spaces.

1 Reply

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

Answer by clunk47 · Sep 23, 2013 at 06:33 PM

Have a look at Transform.Find.

In this example, I create a list of planets, and a list of children of the planets by first finding all transforms in the scene, then telling the code that planets are transforms without parents, but that have children. The list of children is found by getting the children of those planets.

C# Example

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class example : MonoBehaviour 
 {
     Object[] Transforms;
     List<Transform> planets;
     List<Transform> children;
     
     void Awake()
     {
         planets = new List<Transform>();
         children = new List<Transform>();
         Transforms = GameObject.FindObjectsOfType(typeof(Transform));
         
         foreach(Transform T in Transforms)
         {
             if(T.parent == null && T.childCount > 0)
             {
                 planets.Add(T);
             }
         }
         
         foreach(Transform planet in planets)
         {
             if(planet.Find ("Cloud1"))
             {
                 Transform cloud1 = planet.Find ("Cloud1");
                 children.Add (cloud1);
             }
         }
         
         if(children.Count > 0)
         {
             foreach(Transform cloud in children)
             {
                 print (cloud.localScale);    
             }
         }
     }
 }
 
 

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 d112570 · Sep 23, 2013 at 06:53 PM 1
Share

Im using a for i = 0 to 10 loop I tried using this

 print (planet.Find("Lava1")).transform.localScale); // example script

it didn't work either. The planet names are random, that's why I use the for loop, to get the name. I use the script to find the planets game object per name and then its children. I can't use the drag game object to inspector. Thanks for your help anyways. It's very much appreciated. I tried this in the beginning, but its too much of a hassle if you make a different solar system, which means you need to go back into the script.

avatar image clunk47 · Sep 23, 2013 at 07:06 PM 1
Share

Edited answer to better suit your situation.

avatar image d112570 · Sep 23, 2013 at 07:23 PM 1
Share

Thankyou so much, you did a lot of work, while you did this, I did the long way around.

 var pCloud1 = GameObject.Find(SystemSizeSetup[i].name + "Cloud1");
 var pCloud2 = GameObject.Find(SystemSizeSetup[i].name + "Cloud2");
 var pLava1  = GameObject.Find(SystemSizeSetup[i].name + "Lava1" );
 var pRing1  = GameObject.Find(SystemSizeSetup[i].name + "Ring1" );
 var pRing2  = GameObject.Find(SystemSizeSetup[i].name + "Ring2" );
 var pRing3  = GameObject.Find(SystemSizeSetup[i].name + "Ring3" );
 var pSphere = GameObject.Find(SystemSizeSetup[i].name + "Sphere");

and the second part is here

 if (pSphere) pSphere.transform.Rotate(Vector3.up * -1*(SystemSizeSetup[i].rotationalSpeed.Surface* Time.deltaTime));
 if (pLava1 ) pLava1. transform.Rotate(Vector3.up * -1*(SystemSizeSetup[i].rotationalSpeed.Surface* Time.deltaTime));
 if (pCloud1) pCloud1.transform.Rotate(Vector3.up * -1*(SystemSizeSetup[i].rotationalSpeed.Cloud1 * Time.deltaTime));
 if (pCloud2) pCloud2.transform.Rotate(Vector3.up * -1*(SystemSizeSetup[i].rotationalSpeed.Cloud2 * Time.deltaTime));
 if (pRing1 ) pRing1. transform.Rotate(Vector3.up * -1*(SystemSizeSetup[i].rotationalSpeed.Ring1  * Time.deltaTime));
 if (pRing2 ) pRing2. transform.Rotate(Vector3.up * -1*(SystemSizeSetup[i].rotationalSpeed.Ring2  * Time.deltaTime));
 if (pRing3 ) pRing3. transform.Rotate(Vector3.up * -1*(SystemSizeSetup[i].rotationalSpeed.Ring3  * Time.deltaTime));

This is what I was trying to do.

I asked this question because I was looking for a way to avoid writing GameObject.Find(SystemSizeSetup[i].name each time. I will look into your code, see if I can learn something from it. $$anonymous$$aybe I can use something in it. Thanks a lot, that was very kind of you.

avatar image clunk47 · Sep 23, 2013 at 07:41 PM 1
Share

If this ends up resolving your concern, please be so kind to upvote (thumbs up) the answer and accept(checkmark). If not, let us know so we can help further. Also, give thumbs up to anyone else's comments that may have helped troubleshoot. $$anonymous$$eep me updated, I'll be on for a while.

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

16 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

Related Questions

GetComponent() - Is it possible to pass a string variable as name of the script? 3 Answers

How can I use GameObject.Find with a string and a variable ? 2 Answers

GameObject.name to string 2 Answers

how can I display a variable as a GUIText 5 Answers

Get Variable From String 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