A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.unity3d.com/Manual/2d-physics/../../ScriptReference/GameObject.GetComponentCount.html below:

Unity - Scripting API: GameObject.GetComponentCount

GameObject.GetComponentCount Suggest a change Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close Your name Your email Suggestion*

Cancel

Switch to Manual

Declarationpublic int GetComponentCount();

Returns

int The number of components on the GameObject as an Integer value.

Description

Retrieves the total number of components currently attached to the GameObject.

You can use GetComponentCount to iterate through component indices, which is especially convenient if used together with GameObject.GetComponentAtIndex. For example, you can iterate through the indices to find the index of a particular component you're interested in and then save the index for later use.

using UnityEngine;

public class IterateComponents : MonoBehaviour { int m_SavedComponentIndex = -1;

void Start() { //Iterate through components on the GameObject for (int i = 0; i < gameObject.GetComponentCount(); i++) { var currComponent = gameObject.GetComponentAtIndex(i); //Check if it is a Rigidbody component if (currComponent.GetType() == typeof(Rigidbody) ) { m_SavedComponentIndex = i; } }

Debug.Log(m_SavedComponentIndex != -1 ? $"Found component at index: {m_SavedComponentIndex}" : "Could not find component"); } }

Additional resources: GameObject.GetComponentAtIndex


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4