- Home /
3D model hiding and re appearing?
Basically I want my game to start with a certain 3D model hidden then if I press a key like h I wan't it to re appear. I am learning unity so I don't know what I am doing really.
Answer by b1gry4n · Oct 15, 2016 at 06:56 PM
https://docs.unity3d.com/ScriptReference/Input.html
https://docs.unity3d.com/ScriptReference/GameObject.SetActive.html
Answer by King_Kurl · Oct 15, 2016 at 07:58 PM
The links posted above are a great place to look and find answers for your questions. This is pretty much how I learnt to code, well that and a lot of google search lol. But since you are a beginner I will give you a really simple code to achieve your goal.
So in your scene all you need to do is attach the following script to any game object, could be an empty game object or the player or whatever you want, and then in the inspector you'll see a variable that says target object. Simply drag the object you want to hide and unhide and drop it in the box. That's it! Now when you run your game the object will start out hidden and will appear/disappear every time you press H. Enjoy!
using UnityEngine;
using System.Collections;
public class EnbaleDisableObj : MonoBehaviour {
public GameObject targetObject;
private bool hideObject;
void Start () {
hideObject = true;
}
void Update () {
if (Input.GetKeyDown (KeyCode.H)) {
hideObject = !hideObject;
}
if (hideObject) {
targetObject.SetActive (false);
} else if (!hideObject) {
targetObject.SetActive (true);
}
}
}
Your answer
Follow this Question
Related Questions
Can I hide/show objects by layer or tags? 3 Answers
hide button 1 Answer
Reloading a Scene throws NullReferenceException on GO with nested Prefabs 0 Answers
show gui when look certain object 2 Answers