Thursday, August 22, 2019

unity - How do I reference one game object in a script attached to another?


This is all in Unity, with C#.


I have attached a script to my player that should destroy a TreeGroup (as in a forest) whenever I press 'Q' and instantiate one when I press 'E'.


Here is the code:



using UnityEngine;
using System.Collections;

public class DisableTrees : MonoBehaviour {

// Use this for initialization
void Start () {

}


// Update is called once per frame
void Update () {

if (Input.GetKeyDown (KeyCode.A)) {
//Disable Trees on A Keypress

Debug.Log("Trees Disabled!");
}

if (Input.GetKeyDown (KeyCode.E)) {

//Enable Trees on E Keypress

Debug.Log("Trees Enabled!");
}

}
}

How do I access and find my 'TreeGroup' object group while script not being attached to 'TreeGroup' but my character instead and then Destroy on Q Keypress? And how do I Instantiate it?




No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...