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 phil420 · Dec 17, 2021 at 02:05 PM · scripting problemcollisiondestroy objectiskinematic

Script to "apply & cancel debuff" effect only restores one value

So basically I have obstacles spawn and when "Player" hits them their speed gets reduced and when the "Enemy" hits them I want him to stop moving for (waitTime); after waitTime the obstacle is destroyed.

For now it works all like a charm only when "Enemy" and "Player" hit the same obstacle, only playerSpeed is restored and on enemy.IsKinematic stays true.

Here is my code:

 private void OnTriggerEnter(Collider other)
 {
     if (other.tag == "Player" && gameManager.isGameActive == true)
     {
         SlowDownPlayer();       
     }
     else if (other.tag == "Enemy" && gameManager.isGameActive == true)
     {
         SlowDownEnemy();
     }
 }

 public void SlowDownPlayer()
 {
     Debug.Log("Splash");
     StartCoroutine(SlowDownInPuddle());
 }

 public IEnumerator SlowDownInPuddle()
 {
     player.gameObject.GetComponent<PlayerController>().playerSpeed = player.gameObject.GetComponent<PlayerController>().playerSpeed / 2;
     yield return new WaitForSecondsRealtime (waitTime);
     Destroy(gameObject);
     if (gameObject != null)
     {
         Debug.Log("player-speed restore");
         player.gameObject.GetComponent<PlayerController>().playerSpeed = player.gameObject.GetComponent<PlayerController>().playerSpeedInit;
     }
 }

 public void SlowDownEnemy()
 {
     Debug.Log("Slurp");
     StartCoroutine(SlowDownInPuddleEnemy());
 }

 public IEnumerator SlowDownInPuddleEnemy()
 {
     enemy.gameObject.GetComponent<Rigidbody>().isKinematic = true;
     yield return new WaitForSeconds(waitTime);
     Destroy(gameObject);
     if (gameObject != null)
     {
         Debug.Log("enemy-kinematic = false");
         enemy.gameObject.GetComponent<Rigidbody>().isKinematic = false;
     }
         
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Hellium · Dec 17, 2021 at 02:32 PM

Something looks wrong with your code. How do you set the value of player and enemy? I believe they are not the references you get from OnTriggerEnter. And destroying the gameObject before the end of the coroutine may abort the coroutine (the destroy happens after the yield so maybe not).

  private void OnTriggerEnter(Collider other)
  {
      if (other.CompareTag("Player") && gameManager.isGameActive)
      {
          StartCoroutine(SlowDownInPuddle(other.gameObject));       
      }
      else if (other.CompareTag("Enemy") && gameManager.isGameActive)
      {
          StartCoroutine(SlowDownInPuddleEnemy(other.gameObject));
      }
  }
  
  private IEnumerator SlowDownInPuddle(GameObject player)
  {
      PlayerController playerController = player.GetComponent<PlayerController>();
      playerController.playerSpeed *= 0.5f;
      yield return new WaitForSecondsRealtime (waitTime);
      Debug.Log("player-speed restore");
      playerController.playerSpeed = playerController.playerSpeedInit;
      Destroy(gameObject);
  }
   private IEnumerator SlowDownInPuddleEnemy(GameObject enemy)
  {
      Rigidbody enemyRigidbody = enemy.GetComponent<Rigidbody>();
      enemyRigidbody.isKinematic = true;
      yield return new WaitForSeconds(waitTime);
      Debug.Log("enemy-kinematic = false");
      enemyRigidbody.isKinematic = false;
      Destroy(gameObject);
  }

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
avatar image
0

Answer by phil420 · Dec 17, 2021 at 03:13 PM

Hey, thanks for your answer! I checked your code and I still have the same error; when player hits the Puddle and Enemy does so too, playerSpeed is restored but enemy.IsKinematic == true. And Vice versa if enemy hits first and player second.

To give you a little bit more context: I have a prefab spawning with the script applied. If either player or enemy hits the obstacle the code worked just fine, just when both are hitting the same object, the first value changed (playerSpeed or IsKinematic) and the second one is not. I feel like it has to do something with the gameObject being destroyed too soon, so I figured I could call if (gameObject != null) and then "isKinematic = false" and "playerSpeed = playerSpeedInit;" which should apply the if statement, when the object is destroyed. I might be mistaken on that one so maybe that's where the problem is. Let me post more of my code:

     {
     gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
     player = GameObject.Find("Player");
     enemy = GameObject.Find("Enemy");
     }
     void Start()
     {
         gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
         player = GameObject.Find("Player");
         enemy = GameObject.Find("Enemy");
     }

Looking forward to your reply and I really hope I or we can solve this :)

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 phil420 · Dec 17, 2021 at 04:06 PM 0
Share

I found a fix for my problem, I think writing it down for you as my "rubber duck" helped me identify the problem better:

I now call Rigidbody enemyRigidbody = enemy.GetComponent(); if (gameObject != null) { enemyRigidbody.isKinematic = false; } in void Update() in addition to the call I make in the respective IEnumerator.

Somehow it broke me GameOver() but that's another story... Thanks again Hellium!

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

334 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 make it so when i shoot a zombie the has been spawned, it will destroy it? 1 Answer

How to trigger a BoxCollider with a RigidBody? 0 Answers

Physics object bouncing on collision. 0 Answers

destructible tilemap, destroying tiles through overlapcircle 1 Answer

Error: An object reference is required to access non-static member, help? 2 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