Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Pick Up and Pass "Ball" CLOSED

Discussion in 'Scripting' started by Master-Antonio, Feb 19, 2015.

  1. Master-Antonio

    Master-Antonio

    Joined:
    Sep 18, 2014
    Posts:
    13
    Sorry for my English . XD XD. I making my 2D Game with Sprites ( RigidBody and Box-Circle Collider ) .
    I'm beginner. So sorry for my error or simple script or class without sense.
    How pick up the Ball with my Character and how pull this? The ball must position themselves to the right or left of my character.
    I thought to put one Box Collider 2D with IsTrigger Actived in Character.
    And when the Ball touches the trigger is added to Character.
    But my Script don't work.

    Code (CSharp):
    1. private GameObject player;
    2. private Ball enemy;
    3. void OnTriggerEnter2D (Collider2D Ball) {
    4.  
    5. if (GameObject.FindWithTag ("Ball"))
    6. player.GetComponentInParent<Ball> ();
    7.  
    8. }

    I created one class called Ball with private GameObject ball and I assigned to a Ball.
    Code (CSharp):
    1. public class Ball : MonoBehaviour {
    2.  
    3.     private GameObject ball;
    4.     // Use this for initialization
    5.     void Start () {
    6.  
    7.     }
    8.  
    9.     // Update is called once per frame
    10.     void Update () {
    11.  
    12.     }
    13. }
    The error on Unity is : NullReferenceException: Object reference not set to an instance of an object
    Grab.OnTriggerEnter2D (UnityEngine.Collider2D Ball) (at Assets/Code/Grab.cs:12)
    Is the player.GetComponentInParent<Ball> ();

    Help me Please.
    Thanks For all.
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,935
    OnTriggerEnter2D already gives you the object that entered it,

    So maybe you want something like,
    Code (CSharp):
    1.  
    2. void OnTriggerEnter2D (Collider2D other)
    3. {
    4. if (other.compareTag("Ball"))
    5. {
    6.   other.transform.parent = player.transform;
    7. // might want to set ball rigidbody to kinematic here also
    8. }
    9. }
     
  3. Master-Antonio

    Master-Antonio

    Joined:
    Sep 18, 2014
    Posts:
    13
    I also tried this method.
    But unity generate an error.

    NullReferenceException: Object reference not set to an instance of an object
    Grab.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/Code/Grab.cs:15)
     
  4. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    Can you post the full class here? The code mgear posted should work, so I think your error is somewhere else in the class.
     
  5. Master-Antonio

    Master-Antonio

    Joined:
    Sep 18, 2014
    Posts:
    13
    In fact I'm going crazy.
    I tried this method yesterday.
    It gave me the exact same error.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5. public class Grab : MonoBehaviour {
    6.     private GameObject player;
    7.  
    8.  
    9.  
    10.  
    11.     void OnTriggerEnter2D (Collider2D other)
    12.     {
    13.         if (other.CompareTag ("Ball"))
    14.         {
    15.             other.transform.parent = player.transform;
    16.            
    17.         }
    18.     }
    19.  
    20. }
     
    Last edited: Feb 20, 2015
  6. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    player is null. If this script is attached to the player gameobject, you can just use "transform" or "gameObject.transform", otherwise, you need to make the player field public, and add the player object via the editor, or use "FindObjectOfType" in the Start Method: http://docs.unity3d.com/ScriptReference/Object.FindObjectOfType.html
     
  7. Master-Antonio

    Master-Antonio

    Joined:
    Sep 18, 2014
    Posts:
    13
    Nothing.
    Same Error.

    Code (CSharp):
    1. public class Grab : MonoBehaviour {
    2.     private Transform player;
    3.  
    4.  
    5.  
    6.  
    7.     void OnTriggerEnter2D (Collider2D other)
    8.     {
    9.         if (other.CompareTag ("Ball"))
    10.         {
    11.             other.transform.parent = player.transform;
    12.          
    13.         }
    14.     }
    15.  
    16. }
    Work In this Mode.
    Code (CSharp):
    1. public class Grab : MonoBehaviour {
    2.     public GameObject player;
    3.  
    4.  
    5.  
    6.  
    7.     void OnTriggerEnter2D (Collider2D other)
    8.     {
    9.         if (other.CompareTag ("Ball"))
    10.         {
    11.             other.transform.parent = player.transform;
    12.            
    13.         }
    14.     }
    15.  
    16. }
    But I wish that the variable was private.
     
    Last edited: Feb 20, 2015
  8. Master-Antonio

    Master-Antonio

    Joined:
    Sep 18, 2014
    Posts:
    13
    Last edited: Feb 20, 2015
  9. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    To what object is the Grab script attached? If it is attached to the player, get rid of the player variable entirely as it is not needed.

    Is there a specific reason why the ball needs to be non-kinematic when attached to the player?
     
  10. Master-Antonio

    Master-Antonio

    Joined:
    Sep 18, 2014
    Posts:
    13

    Is attached to the player, i can not delete public GameObject player; because is used here
    other.transform.parent = player.transform;

    The ball needs to be non-kinematic because must land on terrain .

    I tried to fix in this way.
    1) http://i.gyazo.com/99f89f0441f12d10bb85943e3283f837.png
    2) http://i.gyazo.com/142f93048578c0cc9af1327206345fbe.png

    Now the first problem is solved, but is exit other problem.
    Unity with Script assign the ball only with Circle Collider to the Character, but remains the Ball with RigidBody2D and Circle Collider, so when I go near it, i'm subjected to its mass, its physics.


    P.S
    And I'm not thinking about throwing the ball.
    I believe that to pull the ball, must have a RigidBody2D.
    So will be other problem.
    But i'm going to step.
     
  11. Master-Antonio

    Master-Antonio

    Joined:
    Sep 18, 2014
    Posts:
    13
    Ok, I think I've solved the problem, just like that.
    Code (CSharp):
    1. public class Grab : MonoBehaviour {
    2.     public GameObject player;
    3.     public GameObject BallDestroyed;
    4.  
    5.  
    6.  
    7.  
    8.     void OnTriggerEnter2D (Collider2D other)
    9.     {
    10.         if (other.CompareTag ("Ball"))
    11.         {
    12.             other.transform.parent = player.transform;
    13.             Destroy (BallDestroyed);
    14.         }
    15.     }
    16.  
    17. }
    Now Ball with RigidBody2D and Circle Collider is Deleted when i take the Ball.
    Now I have to place the right and left of the character.
    How do I this?
    And How Shoot the ball?

    Thanks for your help guys.
    Beautiful community.
     
    Last edited: Feb 20, 2015
  12. Master-Antonio

    Master-Antonio

    Joined:
    Sep 18, 2014
    Posts:
    13
    Did All.
    Thanks to Timelog.

    CLOSED
     
  13. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    oops read it wrong disregard this