Trouble Getting My Player Object's Script to interact with My stage's Script

Hello!

I am currently in the works of a small test scene and am having an issue getting a script on an object to be affected by a script on my player character.

I want the function to be as so:

  • If my player meets a certain condition, it turns on a bool and calls a public function in a script attatched to my stage.
  • When the function is called on this script, I’d like for the stage to rotate a certain degree immediately.

So far, I have for the script on the object:

public class GravitySimulator : MonoBehaviour
{
    [SerializeField] private GameObject _stage;

    public void RotateStage(GameObject stage)
    {

        _stage.transform.Rotate(Vector3.forward, 90f);
    }
    public void UnRotateStage(GameObject stage)
    {
        _stage.transform.Rotate(Vector3.forward, -90f);
    }
}

When it comes to the stage script. I tried GetComponent, but that didn’t seem to function, so I added the prefab in instead.

For the PlayerController side, I have:

private GravitySimulator _stageRotation;

private void OnTriggerExit(Collider other)
{
    if (other.gameObject.CompareTag("ZGravity"))
    {
        if(GravityIsAltered == false)
        {
            GravityIsAltered = true;
            _stageRotation.RotateStage();
        }
    }
    else if (other.gameObject.CompareTag("YGravity"))
    {
        if (GravityIsAltered == true)
        {
            GravityIsAltered = false;
            _stageRotation.UnRotateStage();
        }
    }
}

Any help possible would be appreciated! I’m not well versed in how scripts interact with other scripts on other object too indepth.

@Xerionn 1. if both scripts are in the same object
[195225-image-2.png|195225]

This should work

private GravitySimulator _stageRotation;

private void Awake()
{
    _stageRotation = GetComponent<GravitySimulator>();
}

private void OnTriggerExit(Collider other)
{
    if (other.gameObject.CompareTag("ZGravity"))
    {
        if(GravityIsAltered == false)
        {
            GravityIsAltered = true;
            _stageRotation.RotateStage();
        }
    }
    else if (other.gameObject.CompareTag("YGravity"))
    {
        if (GravityIsAltered == true)
        {
            GravityIsAltered = false;
            _stageRotation.UnRotateStage();
        }
    }
}

2.If the gravity simulator script is in a child or parent of the object where the player controller script is located

this should work

private GravitySimulator _stageRotation;

private void Awake()
{
    //if child
    _stageRotation = GetComponentInChildren<GravitySimulator>();
    
    //if parent
    _stageRotation = GetComponentInParent<GravitySimulator>();
}

private void OnTriggerExit(Collider other)
{
    if (other.gameObject.CompareTag("ZGravity"))
    {
        if(GravityIsAltered == false)
        {
            GravityIsAltered = true;
            _stageRotation.RotateStage();
        }
    }
    else if (other.gameObject.CompareTag("YGravity"))
    {
        if (GravityIsAltered == true)
        {
            GravityIsAltered = false;
            _stageRotation.UnRotateStage();
        }
    }
}

3.If none of them above there is 2 more way

3.1. Serialize _stageRotation and assign script

[195226-image-3.png|195226]

[SerializeField]
private GravitySimulator stageRotation;

private void OnTriggerExit(Collider other)
{
    if (other.gameObject.CompareTag("ZGravity"))
    {
        if(GravityIsAltered == false)
        {
            GravityIsAltered = true;
            stageRotation.RotateStage();
        }
    }
    else if (other.gameObject.CompareTag("YGravity"))
    {
        if (GravityIsAltered == true)
        {
            GravityIsAltered = false;
            stageRotation.UnRotateStage();
        }
    }
}

3.2. Find inside scene(highly not recommended)

private GravitySimulator _stageRotation;

private void Awake()
{
    _stageRotation = FindObjectOfType<GravitySimulator>();
}

private void OnTriggerExit(Collider other)
{
    if (other.gameObject.CompareTag("ZGravity"))
    {
        if(GravityIsAltered == false)
        {
            GravityIsAltered = true;
            _stageRotation.RotateStage();
        }
    }
    else if (other.gameObject.CompareTag("YGravity"))
    {
        if (GravityIsAltered == true)
        {
            GravityIsAltered = false;
            _stageRotation.UnRotateStage();
        }
    }