So I am making a Game Tutorial and I want to activate and deactivate parts of the scene depending on what I need. To disable all the buttons I made Object.Find("Add +1").SetActive(false); and then a debug to see that its working. After this later in the script in Game Tutorial wich is the same script I Said Object.Find("Add +1").SetActive(true); But when I tried it didn’t worked and it gave me this error : NullReferenceException: Object Reference not set to an instance of an object
What I can do?
Here is the full script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RoboScript : MonoBehaviour
{
public GameObject[] popUps;
private int popUpIndex;
public float waitTime = 5f;
public int callGloba;
private void Start()
{
GameObject.Find("Add +1").SetActive(false);
Debug.Log("Add Has Been Disabled Succesfully");
GameObject.Find("Extract -1").SetActive(false);
Debug.Log("Extract -1 Has Been Disabled Succesfully");
GameObject.Find("Show Number").SetActive(false);
Debug.Log("Show Number Has Been Disabled Succesfully");
}
private void Update()
{
callGloba = Level2.yos;
for (int i = 0; i < popUps.Length; i++)
{
if(i == popUpIndex)
{
popUps[i].SetActive(true);
} else
{
popUps[i].SetActive(false);
}
}
if (popUpIndex == 0)
{
if (Input.GetMouseButtonDown(0))
{
popUpIndex++;
}
} else if (popUpIndex == 1)
{
GameObject.Find("Add +1").SetActive(true);
Debug.Log("Add Has Been Enabled Succesfully");
if (callGloba >= 1)
{
popUpIndex++;
}
}else if (popUpIndex == 2)
{
if (Input.GetMouseButtonDown(0))
{
popUpIndex++;
}
}else if (popUpIndex == 3)
{
if (callGloba == 0)
{
popUpIndex++;
}
}
else if (popUpIndex == 4)
{
if (callGloba >= 10)
{
popUpIndex++;
}
}
}
}
yes for example you can add on the top part of your script:
public class RoboScript : MonoBehaviour
{
public GameObject[] popUps;
private int popUpIndex;
public float waitTime = 5f;
public int callGloba;
//
public GameObject addObj;
void Awake(){
addObj = GameObject.Find("Add +1");
}
and then replace all your GameObject.Find(“Add +1”); with
correct! be careful if you use this method the objects must start as active before you start the game. Because of that reason that gameobject.find only tracks active objects.
If you want an even better way you can drag them in the inspector and you dont need to set them in void awake
I am doing a Game Tutorial as I said and as you saw in the script in one line it says
if (Input.GetMouseButtonDown(0))
That is not from the first line. Anyway, the thing is that in the tutorial you need to press on a button but if I do with Input if you press anywhere on the screen you will get it done. How do I make it so that I can only press on the specific button if you want as reference the button is obj 3
hmm for clicking on buttons it would be better if you used the canvas UI feature, it is much better for buttons here is a nice tutorial:
if you still want to keep the same style as you have then you would need to put a collider on the obj3, and use a raycast from your mouse position and if it detects collision with your button it would trigger. But the canvas UI is much easier and better way aswell =)
Also I am running into a problem with the script you just sent me when i put inside the code for clicking obj3 i put popupindex++; so it continues but when I press on the button it gets stuck there
The script doesn’t continue as intended. After its done with Input MouseButton (0) it should continue at the else if but insted its stuck where I put at Debug.Log(“Hi”) and it doesn’t continue What should I do?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RoboScript : MonoBehaviour
{
public GameObject[] popUps;
private int popUpIndex;
public float waitTime = 5f;
public int callGloba;
public GameObject addObj;
public GameObject addObj1;
public GameObject addObj2;
void Start()
{
addObj.SetActive(false);
addObj1.SetActive(false);
addObj2.SetActive(false);
}
private void Update()
{
callGloba = Level2.yos;
for (int i = 0; i < popUps.Length; i++)
{
if(i == popUpIndex)
{
popUps[i].SetActive(true);
} else
{
popUps[i].SetActive(false);
}
}
if (popUpIndex == 0)
{
if (Input.GetMouseButtonDown(0))
{
popUpIndex++;
}
} else if (popUpIndex == 1)
{
addObj.SetActive(true);
if (callGloba >= 1)
{
popUpIndex++;
}
}else if (popUpIndex == 2)
{
addObj.SetActive(false);
addObj2.SetActive(true);
if (Input.GetMouseButtonDown(0))
{
Vector2 worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(worldPoint, Vector2.zero);
if (hit.collider == addObj2)
{
popUpIndex++;
return;
}
Debug.Log("Hi");
}
}else if (popUpIndex == 3)
{
addObj.SetActive(false);
addObj1.SetActive(true);
if (callGloba == 0)
{
popUpIndex++;
}
}
else if (popUpIndex == 4)
{
if (callGloba >= 10)
{
popUpIndex++;
}
}
}
}