Forum rules - please read before posting.

Having a gameobject disabled at the start of a scene - what's best practice?

Unity Ver: 2019.4.8f1
AC Ver: 1.71.8

Hello,

I'm just after best practice for having a gameobject disabled at the start of a scene, which would then be enabled later on.

Previously, I've been disabling the object in the scene via the inspector and then using Object:Call Event to turn it on, however I'm sure I've caught posts here that suggest that isn't considered the safe way to do things.

Alternatively, there is a Remember Visibility component that can be attached to the object and the 'visibility on start' set to 'off'. That's good, however the collision of the object remains. In my case I want the object to be disabled entirely - no visuals, no logic and no collision active on it.

What's the appropriate way of doing this?

Comments

  • edited September 2020

    It mainly depends on whether or not you want to record its on/off state in save games.

    If so, then disabling it can prevent it from being "found" by the save system when attempting to update it upon loading.

    The easiest way is to just keep it hidden from view, by e.g. placing it under the floor. An Object: Teleport Action can then place it in view when ready, and the Remember Transform Action can keep track of its position.

    Slightly more complicated alternatives include:

    1. Relying on two Animation states to control the enabled state of its Renderer and Collider components, and then using animation playback to control its state. The Remember Animator component can be used to save this state.
    2. Making the object a prefab, and spawning it in when needed with the Object: Add or remove Action. Saving this state involves making the prefab's name unique, attaching a Remember Transform component, and placing it in a Resources folder - see the Manual's "Saving asset references" chapter for more on this.
  • Morning Chris,

    Indeed in my case the recording of it's state would be important, so that's good. That means inspector disabling is a total no go from now on.

    I love the Object: Teleport idea!

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.