How to compare two objects?
Hello there, I'm having a problem with my game. I want the player when he hits the Keyword T and has the ball in his hands to throw it. My problem what I have it's that even tho the player doesn't have the ball but hits T the Ball respawn in the Y axis like he's accepting the if statement.
I currently have this code in a Update function, there is a picture below that shows you when you press T and the ball respawn on top. I appreciated the help Thank You.
     if(Input.GetKeyDown(KeyCode.T) && player != null)
     {
        
         ball.transform.parent = null;
         ball.transform.position = new Vector3(transform.position.x, transform.position.y + movespeed, 1);
         ball.GetComponent<Collider>().enabled = true;
     }
 }
 
               
Where are you setting player null to try to prevent this?
Hi there thank you for the reply, I think I don't have the player setting to null. I though for default if the player isn't having anything on his hand it will be null. So no I don't have the player = null anywhere if that what you are asking. Here are the codes that I have if you care to view and reply This is where the player does all moving, pressing any $$anonymous$$ey etc:
public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour {
 [HideInInspector]
 public bool facingRight = true;
 [HideInInspector]
 public bool jump = false;
 public float moveForce = 365f;
 public float maxSpeed = 5f;
 public float jumpForce = 1000f;
 public Transform groundCheckR;
 public Transform groundCheckL;
 private bool groundedL = false;
 private bool groundedR = false;
 private Animator anim;
 private Rigidbody rb2d;
 public GameObject player;
 public GameObject ball;
 public float movespeed = 1.0f;
 // Use this for initialization
 void Awake()
 {
    
     anim = GetComponent<Animator>();
     rb2d = GetComponent<Rigidbody>();
 }
 // Update is called once per frame
 void Update()
 {
     groundedL = Physics.Linecast(transform.position, groundCheckL.position, 1 << Layer$$anonymous$$ask.NameToLayer("Ground"));
     groundedR = Physics.Linecast(transform.position, groundCheckR.position, 1 << Layer$$anonymous$$ask.NameToLayer("Ground"));
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space) && groundedL)
     {
         jump = true; 
     }
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space) && groundedR)
     {
         jump = true;
     }
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.RightArrow))
     { transform.eulerAngles = new Vector3(0,90,0); }
     if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftArrow))
     { transform.eulerAngles = new Vector3(0, -90, 0); }
     if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.T) && player.GetInstanceID() != ball.GetInstanceID())
     {
         //ball.GetComponent<Collider>().enabled = true;
         ball.transform.parent = null;
         ball.transform.position = new Vector3(transform.position.x, transform.position.y + movespeed, 1);
         ball.GetComponent<Collider>().enabled = true;
     }
 }
                  Your answer
 
             Follow this Question
Related Questions
See if function returns a certain gameObject 0 Answers
How to check if any GameObject equals any other GameObject 0 Answers
How should i go about having an object have 2 clickable areas? 2 Answers
UnityScript nested objects best practice 1 Answer
How do i make object when collided be cutted/culled? 0 Answers