SHARE
TWEET

Untitled

a guest Aug 24th, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Class1 : MonoBehaviour
  6.  
  7. //Option 1, to instantiate a prefab on command
  8.  
  9. [SerializeField]
  10. private GameObject _gameObjectPrefab; //assign prefab in the Inspector
  11.  
  12. void Start()
  13. {
  14.  
  15. }
  16.  
  17. void Update()
  18. {
  19.     if (_gameObjectPrefab != null)
  20.     {
  21.         if (Input.GetKeyDown("Space")
  22.         {
  23.             Instantiate(_gameObjectPrefab, transform.position, Quaternion.identity);
  24.         }
  25.     }
  26. }
  27.  
  28. //Option 2, to access a component on a GameObject in the hierarchy
  29.  
  30. public class Class2 : MonoBehaviour
  31.  
  32. private GameObject _gameObject;
  33.  
  34. void Start()
  35. {
  36.     _gameObject = GameObject.Find("GameObject Name").GetComponent<ComponentName>();
  37.  
  38.     if (_gameObject == null)
  39.     {
  40.         Debug.LogError("GameObject is NULL!);
  41.     }
  42. }
  43.  
  44. void Update()
  45. {
  46.     if (Input.GetKeyDown("Space")
  47.         {
  48.             _gameObject.ExternalFunction();
  49.         }
  50. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top