Answers for "unity get component from gameobject"

1

unity get all components in gameobject

GetComponents<Component>()
Posted by: Guest on December-27-2020
7

unity get component

using UnityEngine;

public class TryGetComponentExample : MonoBehaviour
{
    void Start()
    {
    	// Since Unity  2019.2 you can use TryGetComponent to check
        // if an object has a component, it will not allocate GC in 
        // the editor if the object doesn't have one.
        if (TryGetComponent(out Rigidbody rigidFound))
        {
        	// Deactivate rigidbody
            rigidFound.enabled = false;
        }
        
		// For versions below 2019.2 you can do it this way:
        // Create a variable
        Rigidbody rigidFound = GetComponent<Rigidbody>();
        
        // If the 'Rigidbody' exist in the gameobject
        if(rigidFound != null)
        {
        	// Deactivate rigidbody
            rigidFound.enabled = false;
        }
	}
}
Posted by: Guest on April-27-2020
1

unity create gameobject with component

GameObject go = new GameObject("Test", typeof(MeshRenderer), typeof(MeshFilter), typeof(YourScript));
Posted by: Guest on July-16-2020

Code answers related to "unity get component from gameobject"

Browse Popular Code Answers by Language