How to use IF statement ONLY when a certain gameobject is in the scene?
Hello!
I have a gameobject with this script attached to it, and it is marked DontDestroyOnLoad:
using UnityEngine;
using System.Collections;
public class PlanetoMapSelect : MonoBehaviour
{
public int PlaneOneSelected;
public int PlaneTwoSelected;
public Transform ScriptObject;
public GameObject LoadPlane;
// Use this for initialization
void Start()
{
Object.DontDestroyOnLoad(ScriptObject);
}
// if (LoadPlane is in scene)
//{
//load this....
//}
public void SelectPlaneOne()
{
PlaneOneSelected = 1;
Debug.Log("Plane One Selected");
Application.LoadLevel(2);
}
public void SelectPlaneTwo()
{
PlaneTwoSelected = 1;
Debug.Log("Plane Two Selected");
Application.LoadLevel(2);
}
}
So it works great, however I need an IF statement to load an object ONLY if the LoadPlane object is in the scene (as seen in the script comments) How do I get the script to recognize that the LoadPlane is in the scene, and get an IF statement to work?
Thank you!
Answer by LockBros · Jan 26, 2016 at 12:02 AM
findLoadPlane = GameObject.Find("LoadPlane");
if(findLoadPlane = null){
//DoNothing
}else{
//load this...
}
The null means that if the game object is not found then it will do the following, if you leave it blank then it will do nothing. The else will happen if it finds the game object and under else is where you write what you want to happen when you find the object.
Your answer
Follow this Question
Related Questions
ScriptName versus PingPongScriptName? 0 Answers
Write values on an instantiated Gameobject ? 1 Answer
Why does Unity lock the transform of prefabs in my scene in playmode? 0 Answers
Using a specific function from a generic object. 0 Answers
How to instantiate a game object every time when space key is pressed 2 Answers