- Home /
gameObject.SetActive(false); not working as intended
So I am trying to make my coins disappear.
PickUpPoints.cs
IEnumerator waitOneSecond(GameObject targetDisable)
{
yield return new WaitForSeconds (1f); //wait 1 second
targetDisable.SetActive (false); //disables coin particles
theTimeCounter.DestroyCoin (); //destroy coin (event in another script)
}
TimeCounter.cs
public class TimeCounter : MonoBehaviour {
public void DestroyCoin () {
gameObject.SetActive (false);
Debug.Log ("destroyd");
}
}
The Debug.Log message is shown which means it 'works' but gameObject is still shown.
Both of the scripts belong to a prefab coin (as shown in the badly cropped image below)
Any solutions to why it won't disappear and how to make it disappear itself? ~Thanks
Inside PickUpPoints.cs
public TimeCounter theTimeCounter;
theTimeCounter = GetComponent<TimeCounter>();
idk if its correct or not so @$$anonymous$$
Answer by game4444 · Sep 07, 2017 at 07:25 AM
Is there another script enabling coin? Please checkout. Your code looks fine. It should disable gameObject.
Turns out there was a GroundDestroyer script attached to it which was supposed to make the coins which weren't collected dissapear but it shouldn't interfere with this.
GroundDestroyer.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GroundDestroyer : $$anonymous$$onoBehaviour {
public GameObject groundDestructionPoint;
// Use this for initialization
void Start () {
groundDestructionPoint = GameObject.Find ("GroundDestructionPoint");
}
// Update is called once per frame
void Update () {
if (transform.position.x < groundDestructionPoint.transform.position.x)
{
//Destroy (gameObject);
gameObject.SetActive (false);
}
}
}
I had the same problem - of course there was another script enabling the thing which I had such bad luck getting to go away.
Thank you for your comment - after reading it I went back through, found the offending script, and tweaked it successfully.
Answer by markusarilius · Sep 07, 2017 at 07:10 AM
I may be wrong but I don't see anything passed to the destroy coin function. It may have been passed some other way but unless it has what is it destroying if it hasn't been passed something?
What do you mean? I can't understand your question but I'm gonna try to elaborate how I think this works.
In PickUpPoints.cs
theTimeCounter.DestroyCoin ();
triggers
In TimeCounter.cs
public void DestroyCoin () {
gameObject.SetActive (false);
Debug.Log ("destroyd");
If I'm not wrong, gameObject (lower case g) means the object the script is attached to (correct me if I'm wrong .-.) so technically this should work?
but it doesn't sooo (
Answer by Purplepigfarm · Sep 08, 2017 at 08:48 PM
I have the same problem. Trying to disable a gameobject with gameObject.SetActive(false); but to no avail. Not accessing it from any other script either.
I am having a similar problem but the problem is to get a non-active child gameobject TO appear, not to disappear. I mention this because I wonder if the problem might be more precisely stated that .SetActive() sometimes isn't working as expected.
$$anonymous$$y question was "Is there a specific technique you have to use to .SetActive() a child GameObject of a GameObject you created using Resources.Load?", so you can look at what I said about the problem. I asked the question a little earlier today.
Are you calling .SetActive(false) inside a method in a class that is $$anonymous$$onobehavior? I have noticed that some Unity commands must be made in that class and won't work if you jump outside $$anonymous$$onobehavior to some other C# class that does NOT inherit from $$anonymous$$onobehavior and try to do the command there. Instantiate(Resources.Load()) and Destroy() are an examples of commands that will fail if you do it outside $$anonymous$$onobehavior. Unless I am somehow wrong about that.
Did you create your coins using instantiate(Resources.Load())? If there were and you created them outside a $$anonymous$$onobehavior class then maybe you could try jumping to inside a method of some $$anonymous$$onobehavior based class and create your coins there (THEN try to disappear them).
Your answer
Follow this Question
Related Questions
Pooling GameObjects with TextMesh ? 0 Answers
Can't Return the Tag of a Child Object 4 Answers
Removing objects from an array 2 Answers
How to remove objects from a list ? 3 Answers
object not spawning but no error message 2 Answers