- Home /
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);
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.
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.
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'?
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.
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.
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);
}
}
}
}
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.
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.
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.