Forum rules - please read before posting.

Calling events to disable a component in a prefab (player switching, custom controller)

Hi there.

I am doing some tests with AC and trying to make a character that is able to "enter" a vehicle. The way I'm doing it is to switch the player to a Car object that has a car controller I got from the asset store.

I have two Action Inputs set up. One to switch to the character, and one to switch to the car. It works, but since the car controller is unrelated, the car continues to respond to WASD movement even if the player has been switched. Which is normal so far.

So after looking into it a bit, I've been trying to use the Object > Call Event action in my Action Inputs to try to enable and disable the Car controller component in the Car object. However since I can only select prefabs from the assets, and not from the scene, the function only affects the Car object in the assets, and not the spawned prefab in the scene. I was wondering if there was some way to make that happen.

Some notes:

Unity version: 2021.3.11f1 (LTS)
AC version 1.77.4 (current)
3D game with direct movement and third person camera.

Both "characters" are prefabs in the assets. The scene I'm testing on does not have them, and they are spawned on the PlayerStart marker.

If I select the prefab in the assets, the component does switch on and off. But the prefab in the scene does not.

Disabling the component manually in the scene does achieve the effect I'm looking for.

The car object has both its own car controller and the Player component, but the latter is set to Motion Control > Manual, so it is only used for player switching.

Comments

  • The Object: Call event Action relies on Unity's event system, which will always refer to the assigned object - a prefab reference won't be converted to a "scene instance" at runtime.

    Instead, you'll need to rely on scripting to affect the component manually. You can use KickStarter.player to get a reference to the current Player, e.g:

    public void EnableCarController ()
    {
        KickStarter.player.GetComponent<CarController> ().enabled = true;
    }
    

    This could be attached to a prefab, and then be triggered with the Object: Call event Action.

    However, it might be better to instead have the state-change automated when the Player-switch occurs - with no need for further Actions after the Player: Switch. This can be done by hooking into the OnSetPlayer custom event.

    Such a script would be attached to the "Car" Player, and would kick in upon switching - whether through Actions, or from loading the scene/save file:

    using UnityEngine;
    using AC;
    
    public class CarStateSetter : MonoBehaviour
    {
    
        private void OnEnable () { EventManager.OnSetPlayer += OnSetPlayer; }
        private void OnDisable () { EventManager.OnSetPlayer -= OnSetPlayer; }
    
        private void OnSetPlayer (Player player)
        {
            bool isMe = (player.gameObject == gameObject);
            GetComponent<CarController> ().enabled = isMe;
        }
    
    }
    

    You'll need to replace "CarController" with the name of the actual Car Controller component involved, but that should automate its enabled state when both switching to and from that Player.

  • Wonderful! The script works as intended. Thank you very much for this.

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.