#Getting Component from an Object in root

1 messages · Page 1 of 1 (latest)

topaz jolt
#

Hi everybody! I could use some help. I am trying to find an object in my scene root typeof(BoxPlayer). I have my class GameManager set to [ExecuteAlways]. My GameManager and my BoxPlayer are both in the scene root. I want to have Unity look for and assign the BoxPlayer player to be my BoxPlayer in the root, which is the character the player plays.

#
[ExecuteAlways]
public class Succubus : BoxPlayer
{
    new protected void Start()
    {
        base.Start();
        if (data.courtesy)
        {
            SpriteRenderer.color = Color.black;
        }
    }
}
#
public abstract class PlatformerManager : Manager
{
    [Tooltip("Defaults to the BoxPlayer in the root.")]
    public BoxPlayer player;
    ...
    new protected void Awake()
    {
        base.Awake();
        SetPlayer();
    }
    new protected void Start()
    {
          base.Start();
          SetPlayer();
          ...
    }
    
    void SetPlayer()
    {
          if (player == null)
          {
              throw new Exception("Player is not defined. Add a player to the root");
          }
          else
          {
              player = (BoxPlayer)FindFirstObjectByType(typeof(BoxPlayer));
          }
    }
    ...
}
#

I thought that using FindObjectByType, or FindFirstObjectByType, or GetComponentInParent would do this, but it doesn't seem to. This is what happens when the Awake method tries to look for the player.

#
Exception: Player is not defined. Add a player to the root
DMBTools.PlatformerManager.SetPlayer () (at Assets/DMBTools/2DUnityUtils/Scripts/Managers/ManagerPlatformer.cs:46)```
#

The actual GameObject for my GameManager is extended to SuccubusManager.cs and that is empty.

public class SuccubusManager : PlatformerManager
{
}
#

Here is my heirarchy

#

I am thinking that my succubus may need to called as a Succubus even though it extends BoxCharacter.