Referencing stuff

So I have a prefab that chases my player.
I couldnt just make a public GameObject player; and drag it into the slot in the inspector

instead i had to do this:

public GameObject target;

in my start i have this
target = GameObject.FindWithTag(“Player”);

and in my chase fucntion:
dist = Vector3.Distance(target.transform.position, gameObject.transform.position);

Now im trying to get a raycast2d to hit the player. But i cant just drag him into the playerCollider.

so how do i get
target = GameObject.FindWithTag(“Player”);
but for a collider?

i tried making
public Collider2D targetCollider;

then in start
targetCollider.GetComponent();
but it just throws an error.

I know what has to happen, just dont know how to make it so.

How can i get the same result as this target = GameObject.FindWithTag(“Player”); but for a collider
I hope you understand what i mean.

The RaycastHit2D object that Raycast2D returns contains the collider that the raycast hit. Is that what you’re looking for?

Right so i dont have to get a variable containing the player collider to do an if check to see if its hitting the the player.

if i do a Debug.Log(hit.collider);

it tells me what the raycast is hitting alright
when my player gets hit it says Player, cool

but i cant do a

if (hit.collider == player) {
blow him the hell up
}

how can i get a comparrison going?

This sorted it

if (hit.collider.CompareTag(“Player”))

which leads me to my next question. How can I find all the information available to something? ie what i can use with what? when i google the docs it doesnt really show everything.

You could read the docs instead of googling the docs. :slight_smile:

That’s like asking “How can I know everything?” It’s sorta not possible. Keep looking and learning and trying stuff.

lol smart Alec!!! you made me laugh out loud!!!

“How can I find all the information available to something?” yes not very clear, i admiit it.

thanks

The list of UnityEngine–>Classes in the Documentation is where I find nearly all my answers. The rest are on various forums.

Unity - Scripting API: Collider2D (unity3d.com)

Check the properties and methods for each class. Yes, there are a lot of them! :slight_smile:

why am i getting

NullReferenceException: Object reference not set to an instance of an object

pointing to this line
if (hit.collider.CompareTag(“Player”))

its working the way i want, it is colliding with a collider i used to stop my player leaving the screen though.

Are you checking to make sure your raycast actually hit something? If the raycast missed, hit.collider will be null.

Tanks Seejay

Hey Paetor

Does it matter if im destryoing the object as soon as it leaves the screen? like 3 seconds later.

“Are you checking to make sure your raycast actually hit something? If the raycast missed, hit.collider will be null.”

ummmmm, how do iyou say, hey raycast, if you didnt hit anything, dont worry about it.

if(hit.collider == null) {
  //The raycast hit nothing.
}

if(hit.collider != null) {
  //The raycast hit something.
}
2 Likes

lets just assume im an idiot, which i am

if (hit.collider.CompareTag(“Player”)) *isnt this line determining if the raycast is hitting the Player?
{
launchRocket = true;
}

if (hit.collider == null)
{
//The raycast hit nothing. *so what needs to go in here?
}

if (hit.collider != null)
{
//The raycast hit something. and here?
}

can somebody please clear this up for me.

You’ll want to check if the raycast has hit something first before checking if it hit the player:

if(hit.collider != null) {
  //The raycast hit something, now lets check if it hit the player.

   if(hit.collider.CompareTag("Player")) {
      //The player was hit.
   }
}

Otherwise if it hits nothing, collider.CompareTag() won’t work because there is no collider to compare.

oooooooooooooooooooooooh k! thanks yes that makes perfect sense, thanks!