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 /
avatar image
0
Question by zeref_slayer · Nov 09, 2021 at 02:15 AM · collider2dspriterenderer

The object spriteRender and collision is off

Hi, I making a platform game that when the player jump on the platform after a few second the platform spriterender and collision is off and after few second after that the platform spriterender and collision is on again.

this is my code for the platform

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Platform : MonoBehaviour
 {
     BoxCollider2D box2D;
     SpriteRenderer platformSp;
     public float timeToSpawn;
 
     public bool platformDown;
 
     // Start is called before the first frame update
     void Start()
     {
         box2D = GetComponent<BoxCollider2D>();
         platformSp = GetComponent<SpriteRenderer>();
     }
 
     private void Update()
     {
         if(platformDown)
         {
             platformSp.enabled = true;
             box2D.enabled = true;
         }
     }
 
     private void OnCollisionEnter2D(Collision2D other)
     {
         if (other.gameObject.CompareTag("Player") && !platformDown)
         {
             StartCoroutine(PlatformFall());
             platformDown = true;
         }
        
         
         else if(platformDown)
         {
             StartCoroutine(PlatformSpawn());
         }
     }
 
     IEnumerator PlatformFall()
     {
         yield return new WaitForSeconds(1f);
         platformSp.enabled = false;
         box2D.enabled = false;
     }
 
     IEnumerator PlatformSpawn()
     {
         yield return new WaitForSeconds(timeToSpawn);
         platformDown = false;
     }
 }

there no error and I dont know what to do I appreciate any help

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

Answer by jmfp92 · Nov 09, 2021 at 09:33 AM

It is because you literally wrote the code to do exactly what you're asking about. I think I'm a little confused on the question because from the code provided you seem to be intentionally using coroutines to turn the boxcollider and spriterenderer off every few seconds. When you set the enabled value to the boxcollider and spriterenderer to false, don't do that anymore. Then in your update function you are turning them back on again once they fall. Assuming the platform is just supposed to fall after a few seconds of the player being on it, why not just destroy the gameobjects this script is attached to instead of disabling it? are you reusing the same platform?

Comment
Add comment · Show 3 · 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 zeref_slayer · Nov 09, 2021 at 09:40 AM 0
Share

Yes I using the same platform gameobject because if I destroy the platform I encounter a few more problem that why I choose to this instead

avatar image jmfp92 zeref_slayer · Nov 09, 2021 at 10:05 AM 0
Share

Ok try this then. Create your platform, add the components you need for it, save it as a prefab in your project, and instead of reusing the same gameobject Instantiate a new object from the prefab. I don't want to insult your intelligence or experience but if you don't know how to instantiate objects in unity you should read some docs before proceeding https://docs.unity3d.com/ScriptReference/Object.Instantiate.html

avatar image zeref_slayer · Nov 09, 2021 at 09:41 AM 0
Share

And thank you for the answer I will give it try

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

134 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

Related Questions

Add PolygonCollider2D from code and ignore sprite mesh 1 Answer

Not sure if I should use UI or SpriteRenderer. need to detect collisions. 1 Answer

Arrays? ↓↓↓↓↓↓↓ 2 Answers

how to make 2d sprite with polygon collider bounce? 1 Answer

Raycast2D Hit Other Object 1 Answer


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