Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by adibak · Jul 08, 2021 at 03:19 AM · not workingscoresetactive

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rage_co · Jul 08, 2021 at 05:17 AM 0
Share

create a void Update and put this in it...

 CheckScore();

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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();
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image adibak · Jul 09, 2021 at 02:28 AM 0
Share

this method worked, thanks

avatar image
1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image adibak · Jul 08, 2021 at 11:35 PM 0
Share

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

avatar image Anonymous620 adibak · Jul 09, 2021 at 01:06 AM 0
Share

have you checked that gameObject.activeSelf is actually false when you cross it

avatar image
0

Answer by Anonymous620 · Jul 08, 2021 at 03:45 AM

Your not calling your CheckScore void

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

128 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

SetActive is giving me a lot of issues 2 Answers

setActive working only with some objects? 1 Answer

Score System not working in Flappy Bird Clone 1 Answer

GameObject[] Issues 1 Answer

GameObject.SetActive not working in Coroutine? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges