GetComponentsInChildren by name - c#

I'm searching for the best way to GetComponentsInChildren by their name. The case is like this, in my GameObject I have a couple of gameobjects that all are named the same.
I want to access all of them by searching for the name and then changing the material of them. The changing material part is already done in the code further down.
gameobject.GetComponentInChildren<Transform>().Find(Name).GetComponent<MeshRenderer>().material = newMaterial;
The upper code worked for getting one of the gameobjects and changing the material. My question is how to get all of the gameobjects and change the material.
(There are not always the same amount of gameobjects)

As you have it now it makes little sense.
GetComponentInChildren returns exactly one single object -> the same this is called on since GetComponentInChildren also looks for the component on itself!
Find again only returns one single object that is somewhere nested below this object.
Why even use GetComponentInChildren<Transform> when you later anyway rather want a MeshRenderer reference?
I think what you are trying to do is rather get all MeshRenderer components, then filter them by name.
foreach(var meshRenderer in gameobject.GetComponentsInChildren<MeshRenderer>(true))
{
if(meshRenderer.name == Name)
{
meshRenderer.material = newMaterial;
}
}
Note the s in GetComponentsInChildren -> This returns ALL MeshRenderer references nested under this object (including this one). By passing in true as parameter this also includes inactive GameObjects and disabled Components.
You could also use Linq Where like
foreach(var meshRenderer in gameobject.GetComponentInChildren<MeshRenderer>(true).Where(meshRenderer => meshRenderer.name == Name))
{
meshRenderer.material = newMaterial;
}

Related

Spawning gameobjects relative to the position, width and length of another gameobject?

I'm currently developing a game in Unity using C# and I've run into a small problem.
I need to spawn a certain gameobjects relative to the Spawnposition and length of another game object. Now I figured that bounds.size would be the best function to use in this instance. As shown bellow I declare first the variable that uses this in my start method:
public void Start()
{
GameObject PointBar = (GameObject) Resources.Load("PointBar Horizontal");
PointBarVectorLength = PointBar.GetComponent<BoxCollider2D>().bounds.size.x;
PointBarVectorConv = Camera.main.WorldToViewportPoint(new Vector2(PointBarVectorLength, 0f));
}
However, the gameobjects in question are inactive at start's call and thus I presume don't return any value for bounds.size when called.
Does anyone know how I can access the bounds.size or equivalent of an inactive gameobject or is there something else I'm missing here?
As noted in the documentation for the Collider.bounds property, bounds "will be an empty bounding box if the collider is disabled or the game object is inactive". So your assumption was pretty much right. Since the object doesn't exist in worldspace that makes sense.
I'm not sure about the most elegant solution for your use case but two options spring to mind.
Calculate the object's spawn dimensions by accessing its transform.localScale and factoring in the scale of prospective parent objects. That could get messy but you could probably also write a helper method to make it more manageable.
Instantiate the object somewhere off screen after you load it, access its Collider.bounds property to get the information you need then move it where ever you want it or store the information for later.
There may be better solutions but that's what leaps to mind for me.
I solved the issue by using GetComponent().bounds.size.x;
instead of BoxCollider. This component can get accessed when the game object is not active.

Why are transforms typically passed when working with in-game objects rather than game objects?

I am well along in learning Unity basics but would like to nail down my understanding of the relation between components and the objects that own them. In the tutorials I've been watching typically use or pass the Transform component when working with objects pulled in code. For example:
void Explode ()
{
Collider[] colliders = Physics.OverlapSphere(transform.position, explosionRadius);
foreach (Collider collider in colliders)
{
if (collider.tag == "Enemy")
{
Damage(collider.transform);
}
}
}
which calls "Damage" with the transform on the colliders it finds:
void Damage (Transform enemy)
{
Enemy e = enemy.GetComponent<Enemy>();
if (e != null)
{
e.TakeDamage(damage);
}
}
Looking at this, it is clear that it is pulling a Component found on all game objects and then using "GetComponent" to pull another component instance by name since the "Enemy" component isn't going to have its own method. Why not just pass collider.gameObject though? I tried this (after changing the Damage to expect a GameObject) and it worked fine.
This seems more intuitive to me but all of the tutorials I've seen use the transform component instead. Does anyone have any insight into why this is so? It would help me deepen my understanding of how Unity structures its code.
I'm not sure what tutorials you are following, but the ones I followed when I first started with Unity used GameObject instead of Transform.
I agree that it is more intuitive to use GameObject. Transform is a part or Component of a GameObject, but since it's mandatory it will always be there, hence the public field .transform. But since a GameObject is technically the holder of all its Componenents, it would be most logical, architecture wise, to pass that as a parameter instead.
At the end of the day, it makes little difference in your examples since you can call a lot of the same methods on both Transform as GameObject.
TLDR:
Use whatever you feel makes most sense.
Short answer
GameObject is more intuitive as you say.
Long answer
I think (my opinion) it's best to pass the most specific and relevant component.
For example, assume you have a function for a character to pick up a crate:
public void PickUp(Transform crate)
{
crate.SetParent(this.transform);
crate.localPosition = Vector3.zero; // or whatever your attachment point is
}
Here it makes sense in my mind that the Transform is passed, because it's the most specific component that will be needed.By passing GameObject here you are only delaying the inevitable GetComponent<Transform> or go.transform.
On the other hand, if you have a function to hide a create then passing the game object would be the bare minimum you need to achieve this:
public void Hide(GameObject crate)
{
crate.SetActive(false);
}
Passing anything else just delays the inevitable x.gameObject
In the explosion example I don't think that I would pass either to be honest. I would probably do this:
void Explode ()
{
Collider[] colliders = Physics.OverlapSphere(transform.position, explosionRadius);
var enemies = colliders
.Select(x => x.GetComponent<Enemy>())
.OfType<Enemy>(); // Or IHealth, IDamageable, ITarget, etc if you have other things that can take damage.
foreach (var enemy in enemies) // If empty, nothing will happen by default
{
enemy.TakeDamage(damage);
}
}
With this approach you can see that there is no need to check tags or nulls. The enemies enumerable is guaranteed to contain either enemies or nothing at all.
By always passing gameObject/transforms you will always have to worry about what it is that you are really receiving at the destination component. You will also open yourself to situations where you are not sure anymore where certain changes to you gameObjects are being made because it can be anything in the system that's making those changes. Your ColorChangerComponent could actually also be moving the object around, destroying some other components, etc. By giving the component a Renderer, it more naturally limits the component to changes on the Renderer only (although you could obviously violate this limitation unless you perform actions against appropriate interfaces).
The only time it really makes sense to pass a generic component is when you are broadcasting this 'event' to a bunch of possible receivers that will each to a different thing with the object. I.e. you can't be certain at compile time what the gameobject will be used for, or you know that it will be used for many different things.
The fact that Unity itself returns Collider[] for the collision check sort of supports this line of thinking, otherwise it would've just returns GameObject[]. The same goes for OnTriggerEnter(Collider collider) or OnCollisionEnter(Collision collision) etc.

How to reach to the child of a game object using FindGameObjectWithTag

In the hierarchy, I have a parent Game Object (named "parent"), which has a child (named "child" and tagged "John"). Initially, The parent is active but the child is inactive.
The parent has a script component, in the Update() function of which I have:
GameObject go = GameObject.FindGameObjectWithTag("John");
go.SetActive(true);
It doesn't work and I get "NullReferenceException: Object reference not set to an instance of an object" in the console. Even if the child is initially activated and I want to deactivate it, it still doesn't work and I get the same error. Why? And is it a good idea to use FindGameObjectWithTag() in Update() in the first place?
In Unity, you can't get an inactive GameObject using any Find function unfortunately.
If you're not creating your GameObject go dynamically, use a public variable and assign it via inspector, it's way better performance wise (as Unity probably uses references), and it's easier (imagine you change a tag, you won't need to check in all your code references to that tag)
As pointed out by Jichael, you cannot use Find functions to get inactive gameobjects.
You can however use:
var yourComponent = GetComponentInChildren<YourComponentType>(true);
If your child object has any component attached to it, you could easilly get it like this. Note the true (boolean) parameter. This is used to indicate that you want to check for inactive gameobjects.
A more detailed example:
var spriteRenderer = GetComponentInChildren<SpriteRenderer>(true);
spriteRenderer.Sprite = yourSpriteVariable;
spriteRenderer.gameObject.SetActive(true);
This is also better performant than using Find functionality, since you only need to look through the child objects of the calling behaviour.

How to get GameObject references

Hi i was wondering if there are other ways to set a GameObject active through code in c#
the only one i know is to do this:
public GameObject box;
box.SetActive(true);
which really great, you can just drop of the object on the inspector but i was wondering if there are other ways
like using a specific name of an object or turning GameObjects into active with a certain tag. I'm having a hard time trying to find an example whenever i google it or maybe i'm searching it wrong
any advice?
Did you have a look at the GameObject API or the Manual?
There are Find, FindWithTag, FindGameObjectsWithTag
or also methods like GetComponentInChildren, FindObjectOfType, FindObjectsOfType
(those you can use to find a certain type of component and thereby there according gameObject you can than set (in)active)
but since you requested activateing multiple objects by tag:
var targetObjects = GameObject.FindGameObjectsWithTag("YourTag");
foreach(var obj in targetObjects)
{
obj.SetActive(true);
}
note however that this is a lot more expensive than having the references from the inspector. So in general try to avoid using these FindXY methods if possible.
And yes how remy_rm pointed out correctly in the comments you definitely should not use them in any update loops, or other methods that get called regularly. You should always call them e.g. in Awake, Start etc and store the references for later
Update
If I understand your comment correctly you additionally have some children in those tagged objects that you also want to set active:
var targetObjects = GameObject.FindGameObjectsWithTag("YourTag");
foreach(var obj in targetObjects)
{
obj.SetActive(true);
// also activate all children
foreach(Transform child in obj.transform)
{
child.gameObject.SetActive(true);
}
}

Hierarchy Climbing and Descending Unity

I have the following hierarchy on my Player prefab for what will be a very simple multiplayer shooter.
It works this way, the Controller object has the scripts that deal with player input, the PlayerShip object has all of the turning, moving, shooting scripts etc and the Camera is just as it sounds, a camera with a few scripts on it for moving it about.
When a new Player is instantiated the Control script needs to locate its relevant PlayerShip, simple enough.
This can be achieved using the following code:
_playerShip = gameObject.transform.parent.gameObject.transform.FindChild("PlayerShip").GetComponent<PlayerShip>();
Which works fine, the only thing is, to me that looks very clunky, inelegant and brittle. Consequently, I'm wondering if there's a better, more efficient, less ugly way of achieving the same thing?
First of all,
_playerShip = gameObject.transform.parent.gameObject.transform.FindChild("playerShip").GetComponent<PlayerShip>();
is redundant. That can be reduced to
_playerShip = gameObject.transform.parent.FindChild("playerShip").GetComponent<PlayerShip>();
or even use '/' just like you do with folder names.
_playerShip = GameObject.Find("Player/playerShip").GetComponent<PlayerShip>();
Now, instead of instantiating the Object and searching for it later on, you can actually instantiate it and retrieve the reference at the-same time.
GameObject obj = Instantiate(prefab,Vector3.zero,Quaternion.identity) as GameObject;
_playerShip = obj.GetComponent<PlayerShip>();
If it is a network game that you instantiate with Network.Instantiate:
GameObject obj = Network.Instantiate(prefab,Vector3.zero,Quaternion.identity,0) as GameObject;
_playerShip = obj.GetComponent<PlayerShip>();
Now, send the PlayerShip reference to the Control script.
If this isn't being called every frame consider this:
_playerShip = GameObject.Find("PlayerShip").GetComponent>PlayerShip>();
Read more about the above here: https://docs.unity3d.com/ScriptReference/GameObject.Find.html
If it is being called every frame look in to this:
https://docs.unity3d.com/ScriptReference/GameObject.FindWithTag.html
Either one should be easier to understand.
EDIT: I may have misunderstood. Would rearranging the hierarchy so that controller is a parent of playerShip? Then you should be able to just look at the child, rather than going through a parent to a sibling.

Categories