- Home /
Cannot deactivate / activate gameobject, even in editor!
Hello,
I have a problem where one of my guns will not deactivate / activate when I switch to it. All of my other weapons will correctly activate / deactivate through my scripts, but just this one weapon refuses to do it!
The weird things is this though: I can't even click on the active checkbox in the editor!
Here is a video showing what happens when I try to deactivate the gameobject -> link to video
I have tried using gameobject.active = false
, gameobject.SetActive(false)
, and also clicking the actual check box in the editor, but to no avail.
Also - I am not using GameObject.Find
, or anything like that. The gameobject is referenced through a script, and is not null
.
Someone help! Is it because of an animation possibly? Its not a referencing issue, because all my other weapons use the same weapon script and they deactivate / activate just fine. Nothing appears in the console log.
Thanks! -Fuego
Answer by keth14 · Oct 29, 2020 at 03:24 AM
Ok, so this may seem weird , but I had the exact same problem although with a child Object whose parent had the animator component along with a script to control it. Even though the animation wasn't playing, I couldn't activate/deactivate the gameobject through a script or in the editor! Tried to fix this by setting the animator component to none, and I can control it again! So I copied the gameobject to an empty scene without a script and only an animator. Turns out the animator was creating the problem -> Link To Video.
So, what I did was, I removed the IsActive property from the animation, and guess what? It started to work normally again -> Link To Video.
I can fix my problem because i am working with a sprite, so I can set the opacity but if this comes up with a gameobject, I wouldn't be able to do this.
Has this been fixed in the latest versions of Unity? I am currently using Unity 2019.3.0f6. Do I have to update the current version? Because I really don't want to as I fear it will mess up my code.
I have a similar problem: the animator doesn't contain any clip that sets the child object acitve. If (and only if) the animatior is active, I cannot setActive(false) its childs! Thanks for the idea: I'll try to make the opacity to 0
This saved me. It was the same cause for me. .active property being used in an animator was blocking the change of active via code at any time, even before triggering the animation that was using the property.
It happened to me too. In my case, I had a PLAYABLE DIRECTOR in the scene, which was preventing SetActive() to work properly. By disabling the director, the problem was solved.
Answer by mtozsahin · Dec 14, 2018 at 08:00 PM
when this annoyance happens to me it usually is that I have some Animation making it enabled. If you have this problem, and can not find the line of code which constantly enables that object, check the animations regarding that object.
Answer by Bunny83 · Mar 07, 2017 at 11:44 AM
Well, you most likely have a script somewhere that is constantly setting it to active. Yes, it's possible that you have created an animation which constantly setting it to active. However from the information given we can't tell.
Thanks for your reply, I thought about that, but I don't think that makes sense though - every single Weapon that I have uses the same Weapon
script, and this is the only Weapon that I have run into this issue with - so if it was somewhere in my code, it would be happening to every single Weapon in the game, not just this one.
Note that i said "a script somewhere". There might be another script that has a reference to your weapon and messing around with it. It doesn't have to be attached to the weapon object.
The easiest way to test this is:
create a prefab out of your weapon (if it isn't one yet)
create a new empty scene and insert the prefab there,
play the scene and see if you can disable it.
If the answer is yes, the problem is in your original scene and not on the weapon itself. As i said for this effect something has to continously setting it to true. So just start up your original scene and disable object by object and always check if you can now disable your weapon.
Answer by ExtinctSpecie · Mar 07, 2017 at 12:47 PM
@Bunny83 may be right or something else requires that component in order to play
Thanks for replying, what do you mean by that? Like a [RequireComponent(typeof(CLASS))]
sort of deal?
The thing is, it isnt happening to any other weapon though...so I really do not think that it has to do with a code issue, otherwise the problem would be consistent throughout every weapon that i have in the game (I have upwards of 6 or 7).
then what you can do is create a copy of that script with another name and check if that will work if it doesnt you can follow the error to find a solution , but its more likely something else depends on that script and thats the reason
Answer by shadowpuppet · Mar 11, 2017 at 10:16 AM
when this annoyance happens to me it usually is that I have some little line of code in an update function making it stay on, or a value or another script that if it is "true" or enableld something is SetActive. I fix it by changing something in that script to generate an error saying such and such script is now broken . This way I locate what script is keeping that object SetActive (true)