Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 sobed434 · Dec 24, 2016 at 06:56 PM · uicollision detectionmovement scriptcollision2djumping object

Pick up code issue

Hi Everyone! I need a little help figuring out this code. For some reason with a collision my object doesn't dissapear/hide.

The code is below:

using System.Collections; using UnityEngine.UI; using UnityEngine;

public class PlayerController : MonoBehaviour {

 public float speed;             //Floating point variable to store the player's movement speed.     
 public float jump;
  float moveVelocity;

      public Text countText;
     public Text winText;
     private int count;
        

 private Rigidbody2D rb2d;       //Store a reference to the Rigidbody2D component required to use 2D Physics.
 

//Grounded Vars bool grounded = true;

 // Use this for initialization
 void Start()
 {
     //Get and store a reference to the Rigidbody2D component so that we can access it.
     rb2d = GetComponent<Rigidbody2D> ();
     count = 0;
     SetCountText ();
     winText.text ="";
 }

void Update () { //Jumping if (Input.GetKeyDown (KeyCode.Space) || Input.GetKeyDown (KeyCode.UpArrow) || Input.GetKeyDown (KeyCode.Z) || Input.GetKeyDown (KeyCode.W)) { if(grounded) { GetComponent ().velocity = new Vector2 (GetComponent ().velocity.x, jump); } }

             moveVelocity = 0;
 
             //Left Right Movement
             if (Input.GetKey (KeyCode.LeftArrow) || Input.GetKey (KeyCode.A))
             {
                     moveVelocity = -speed;
             }
             if (Input.GetKey (KeyCode.RightArrow) || Input.GetKey (KeyCode.D))
             {
                     moveVelocity = speed;
             }
 
             GetComponent<Rigidbody2D> ().velocity = new Vector2 (moveVelocity, GetComponent<Rigidbody2D> ().velocity.y);
 
     }
     // // Check if Grounded
     void OnTriggerEnter2D()
     {
             grounded = true;
     }
     void OnTriggerExit2D()
     {
             grounded = false;
     }



 //FixedUpdate is called at a fixed interval and is independent of frame rate. Put physics code here.
 void FixedUpdate()
 {
     //Store the current horizontal input in the float moveHorizontal.
     float moveHorizontal = Input.GetAxis ("Horizontal");

     //Store the current vertical input in the float moveVertical.
     float moveVertical = Input.GetAxis ("Vertical");

     //Use the two store floats to create a new Vector2 variable movement.
        Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

     //Call the AddForce function of our Rigidbody2D rb2d supplying movement multiplied by speed to move our player.
     rb2d.AddForce (movement * speed);

        // Update is called once per frame. this is where our physics code will go
    


     }

 

 //OnTriggerEnter2D is called whenever this object overlaps with a trigger collider.
 void OnTriggerEnter2D(Collider2D other) 
 {
     //Check the provided Collider2D parameter other to see if it is tagged "PickUp", if it is...
     if (other.gameObject.CompareTag("PickUp"))
             {
                  other.gameObject.SetActive(false);
             }
 }


     void SetCountText ()
     {
     countText.text ="countText:" + count.ToString();
     if (count>=12){
             winText.text= "You Win!";
     }
 }

}

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 PizzaPie · Dec 24, 2016 at 07:56 PM

The problem is that you have 2 OnTriggerEnter2D functions it never calls the second. Instead use only one like this: void OnTriggerEnter2D(Collider2D other) { grounded = true; if (other.gameObject.CompareTag("PickUp")) { other.gameObject.SetActive(false); } } Also when you disable the object it will never call the OnTriggerExit(). Hope that helps.

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

113 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

Related Questions

Collision not working 1 Answer

Collisions happening though disabled in matrix 2 Answers

How do I make my player jump? 1 Answer

Prevent "useless" character movements 0 Answers

Collisions not working in top-down RPG? 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