how to make an game object inactive at a particular time
i have a start camera in which their is an option "tap to start" when taped on screen the game begins. i have a game object added to the scene for this start camera. I need to make the game object deactivate when the the screen is tapped.Can u please suggest a method to do it..i am a beginner in unity.
start camera is actually like a splash screen...like an intro to the game in which some animations are done...all those are inside the game object
i have attached a script to game object
using UnityEngine; using System.Collections;
public class StartCameraBgScript : $$anonymous$$onoBehaviour {
// Use this for initialization
void Start () {
gameObject.SetActive(false);
}
// Update is called once per frame
void Update () {
}
}
//but when i run the game ..from beginning itself the game object is in active...what i need is to make the game object inactive when the screen is touched (the gameobject is actually a start camera, like a splash screen)
Answer by arjunh · Sep 07, 2016 at 09:29 AM
public class StartCameraBgScript : MonoBehaviour {
bool Test = true;
float Timer = 0.0f ;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
/* in this foreach method when the screen is touched the game begins and the game object automatically gets in active */
foreach (Touch touch in Input.touches) {
if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
{
gameObject.SetActive(false);
}
}
/* this method is given for testing in unity game player scince touch doesnot work (only mouse point),this is actually for testing purpose*/
if (Test == true) {
Timer += 1 * Time.deltaTime;
if (Timer >= 5)
{
coinMag = false;
Timer = 0;
gameObject.SetActive(false);
}
}
}
}
Answer by metalted · Sep 07, 2016 at 07:17 AM
You can try to use this method:
https://docs.unity3d.com/ScriptReference/GameObject.SetActive.html
public GameObject cameraHolder;
void Start(){
//Activate
cameraHolder.SetActive(true);
//Deactivate
cameraHolder.SetActive(false);
}
using UnityEngine; using System.Collections;
public class ExampleClass : $$anonymous$$onoBehaviour { void Example() { gameObject.SetActive(false); } }
// my game object name is startcamera , how can i relate it in " gameObject.SetActive(false); "
i have attached a script to game object
using UnityEngine; using System.Collections;
public class StartCameraBgScript : $$anonymous$$onoBehaviour {
// Use this for initialization
void Start () {
gameObject.SetActive(false);
}
// Update is called once per frame
void Update () {
}
}
//but when i run the game ..from beginning itself the game object is in active...what i need is to make the game object inactive when the screen is touched (the gameobject is actually a start camera, like a splash screen)
Your answer
Follow this Question
Related Questions
how make object follow mouse cursor in 3d space 0 Answers
How to destroy only the collided instance of prefab and not the original one? 0 Answers
Cannot convert type `UnityEngine.GameObject[]' to `UnityEngine.GameObject' via a built-in conversion 1 Answer
How to Remove a Button after it has been pressed and executed it's script? 3 Answers