- Home /
Why isn't inactive gameobject enabling itself?
Hi,
I want a gameobject to activate itself once the score reaches 100 or more. I have this script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnableObj : MonoBehaviour
{
public int scoreThreshold;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void CheckScore()
{
if (ScoringSystem.theScore >= scoreThreshold && !gameObject.activeSelf){
Debug.Log("kk");
gameObject.SetActive(true);
}
}
}
(Scoring System is a simple script with the score value and displays it to UI text)
I set the score threshold to 100 in the inspector for that gameobject. When I run the game, however, and the score crosses 100, nothing happens. Can someone please help? thanks.
Answer by Eno-Khaon · Jul 09, 2021 at 01:33 AM
There are quite a few problems here that aren't being addressed well.
First, as has been mentioned, there appears to be nothing actively running that can check the state of the score (i.e. Update() function).
Second, and even more importantly, Update() won't run in a disabled MonoBehaviour script (or, by extension, the GameObject with said script attached to it).
If you want this GameObject to be disabled, then a separate GameObject will (typically) need to be the one that signals this one. For example:
public EnableObj otherObj;
void Update()
{
otherObj.CheckScore(); // this will need to be made public in EnableObj
}
This isn't really a better way of handling this, but it would get the job done.
A somewhat more sensible approach would be to have a separate object enable that one instead, when the conditions are met:
void Update()
{
if (ScoringSystem.theScore >= scoreThreshold && !gameObject.activeSelf)
{
gameObjectToBeMadeVisible.SetActive(true);
}
}
For an even more optimal variant, you would instead want to signal your script whenever the score is updated:
// Whatever the means of this would be...
void GainPoints(int pointsEarned)
{
scoringSystem.theScore += pointsEarned
// Reusing EnableObj example name
otherObj.CheckScore();
}
Answer by starviw · Jul 08, 2021 at 06:31 AM
From the look of your code, it appears that you replace unity's Update() with your CheckScore() and you didn't call CheckScore(). Just add Update() and call CheckScore() in it if you need it as a function or you can just replace it with Update
edit: Missed the part where you use gameobject, which means you are calling the function in the disabled object itself.
Update() does not run on an inactive game object. What you need is another script to do so. It can be the player if you are just activating something on the player, or a manager such as level manager, ui manager, etc. depending on what game object are you trying to activate.
You will need to get the object either by serialize field, get component or find object, and gets the object on start of the game. so you can activate it later. If you are dealing with ui though, instead of gameobject, you can enable/disable the canvas instead as a disabled canvas does not update itself either.
Hi, I added an update function and called checkscore there but the gameobject still isn't activating itself when i cross 100 points, and i also replaced it with the update function and still the same issue...not sure why this is happening
have you checked that gameObject.activeSelf
is actually false when you cross it
Your answer

Follow this Question
Related Questions
SetActive is giving me a lot of issues 2 Answers
setActive working only with some objects? 1 Answer
GameObject[] Issues 1 Answer