Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Class1 : MonoBehaviour
- //Option 1, to instantiate a prefab on command
- [SerializeField]
- private GameObject _gameObjectPrefab; //assign prefab in the Inspector
- void Start()
- {
- }
- void Update()
- {
- if (_gameObjectPrefab != null)
- {
- if (Input.GetKeyDown("Space")
- {
- Instantiate(_gameObjectPrefab, transform.position, Quaternion.identity);
- }
- }
- }
- //Option 2, to access a component on a GameObject in the hierarchy
- public class Class2 : MonoBehaviour
- private GameObject _gameObject;
- void Start()
- {
- _gameObject = GameObject.Find("GameObject Name").GetComponent<ComponentName>();
- if (_gameObject == null)
- {
- Debug.LogError("GameObject is NULL!);
- }
- }
- void Update()
- {
- if (Input.GetKeyDown("Space")
- {
- _gameObject.ExternalFunction();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement