Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Xerionn · Apr 19 at 11:23 AM · rotation3dunityeditorscripting beginnerfirst person controller

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.


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by ogulcan_topsakal · Apr 19 at 12:02 PM

@Xerionn 1. if both scripts are in the same object Example - 1

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

Example 3-1

 [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();
         }
     }
 


image-3.png (77.5 kB)
image-2.png (33.7 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

234 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to only get the negative or positive part of a GetAxis 1 Answer

Standard FPS Controller - Smoothing Movement 2 Answers

How can I rotate my player with a Joystick? 0 Answers

How can i place subjects in different positions? 0 Answers

Hello, I have a problem adding material to an object. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges