- Home /
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.
Answer by ogulcan_topsakal · Apr 19 at 12:02 PM
@Xerionn 1. if both scripts are in the same object
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
[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();
}
}
Your answer
Follow this Question
Related Questions
how to only get the negative or positive part of a GetAxis 1 Answer
change direction by rotating object around y axis and adding relative torque 1 Answer
Rotation Question in Top-down 3D Game 0 Answers
how can i make my enemy face player as well as be parallel to the ground with smooth rotation? 1 Answer
How can I have the turret on top of the parent ship be similar transforms? 0 Answers