Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
  • Help Room /
avatar image
0
Question by Kraschkaboom · Aug 05, 2021 at 12:11 PM · triggerrigidbody2dgravitygameobjects

How do I modify a game object component from a script attached to another game object?

Bare with me, I'm a beginner at both Unity and coding with Unity. I have two game objects, Player and Finish. When reaching the Finish I have a script called LevelTimer attached, where I use OnTriggerEnter2D to stop the timer, this works excellent. But how do I stop the Player object from moving from inside my LevelTimer-script attached to the Finish game object. I was thinking about setting gravity to 0 to accomplish this.

How do I write code to the modify the Player component RigidBody2D and set the Graivty Scale to 0 in the the LevelTimer script?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class LevelTimer : MonoBehaviour
 {
     private bool IsActive;
     private float Timer;
     private string ReadableTimerText;
 
     public Text TimerText; // The visible timer
 
     // Start is called before the first frame update
     void Start()
     {
         IsActive = true;
         Timer = 0f;
     }
 
     // Update is called once per frame
     void Update()
     {
         // check if timer is running
         if( IsActive == true)
         {
             Timer += Time.deltaTime;
 
             if(TimerText != null) TimerText.text = "Time: " + FormatTimer();
 
             Debug.Log( FormatTimer() );
         }
     }
 
     private void StopTimer()
     {
         IsActive = false;
         Debug.Log( "Score: " + FormatTimer() );
     }
 
     private string FormatTimer()
     {
         ReadableTimerText = "";
         int minutes = Mathf.FloorToInt(Timer / 60f);
         int seconds = Mathf.FloorToInt(Timer % 60f);
         int milliseconds = Mathf.FloorToInt((Timer * 100f) % 100f);
         ReadableTimerText = minutes.ToString("00") + ":" + seconds.ToString("00") + "." + milliseconds.ToString("000");
 
         return ReadableTimerText;
     }
 
     private void OnTriggerEnter2D(Collider2D collision)
     {
         if (collision.gameObject.name == "Player")
         {
             IsActive = false;
             StopTimer();
         }
     }
 }
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
0
Best Answer

Answer by Hellium · Aug 05, 2021 at 01:37 PM

  private void OnTriggerEnter2D(Collider2D collision)
  {
      if (collision.gameObject.name == "Player")
      {
          IsActive = false;
          StopTimer();

          // Replace "PlayerScript" by the actual name of your player script
          PlayerScript player = collision.gameObject.GetComponentInChildren<PlayerScript>();
          if(player != null)
                player.DisableControls();
      }
  }

Then, in your player script, implement the DisableControls method with whatever is needed to disable the controls. Since you haven't shown how you did it, I can't help.

 public void DisableControls()
 {
      // Do what is needed
      // May be as simple as `this.enabled = false`
 }

Comment
Add comment · Show 1 · 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
avatar image Kraschkaboom · Aug 05, 2021 at 02:01 PM 0
Share

Just added code below to my Player script to test it out. Exactly what I was looking for. Thank you!

 public void StopMove()
 {
     Debug.Log("Stop moving!");
 }

and this to my leveltimer script

 private void OnTriggerEnter2D(Collider2D collision)
 {
     if (collision.gameObject.name == "Player")
     {
         IsActive = false;    
         StopTimer();
 
         Player player = collision.gameObject.GetComponentInChildren<Player>();
         if (player != null)
                     player.StopMove();
     }
 }

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

166 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

Related Questions

Can't get health/damage to work 0 Answers

How to change the player gravity level?,How to change the value of the players gravity? 1 Answer

HTC vive (VR) blood/flash screen effect. 0 Answers

Rigidbody2D falls slowly with MovePosition? 1 Answer

How to make Smooth Gravity Transitions 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