#Right now you're trying to get the

1 messages · Page 1 of 1 (latest)

gleaming steeple
#

i have the entity script attached to my enemies, this is the script for the gun. i'm trying to figure out how i would write this

pine moat
#

If you want to get the entity from the thing you hit, why not get the component from the thing you hit

rugged oracle
#

writing TryGetComponent, or any method really, on its own results in this.TryGetComponent

pine moat
#

Instead of this

rugged oracle
#

you still have not answered this

pine moat
#

You already know how to get a component from the thing you hit, you're getting the hitbox

gleaming steeple
rugged oracle
#

rays don't hit random components

#

they hit colliders

#

the collider is the "hitbox", i honestly have no idea what this Hitbox component is supposed to be

#

but yeah even in your previous code, you check if the thing you hit has a hitbox component and then try to use the entity component. you just haven't checked for the entity component

gleaming steeple
#

it gets added to every collider when the enemy is spawned in

#

well every limb

rugged oracle
#

...so that has an OnRayCastHit that's calling Entity.TakeDamage, why aren't you using that

gleaming steeple
#

because i'm confused and i'm not sure how to

#

something like this?

rugged oracle
#

you have to actually use the Hitbox you got from GetComponent

#

with GetComponent, you have to receive the return value and then check that

#

alternatively with TryGetComponent, it returns a boolean and gives an out parameter to receive the argument

gleaming steeple
#

so Hitbox.OnRayCastHit instead?

rugged oracle
#

that doesn't do what i said, no

gleaming steeple
#

i don't know what you mean by use the hitbox

rugged oracle
#

use the one you get from GetComponent

#

GetComponent returns a value

#

surely you've used GetComponent before?

gleaming steeple
#

i've used getcomponent before but i'm not sure what you mean

rugged oracle
#

ComponentType componentInstance = targetGameObject.GetComponent<ComponentType>()

gleaming steeple
#

i don't understand how to implement this into my code

#

i'm sorry

#

are you able to tell me what i actually need to write instead of hitbox.onraycasthit? i feel that would help me elarn better, so i can refer back to it in the future if i have the same problem

rugged oracle
#

you would use that same line of code, but you would have to actually grab the hitbox from the GetComponent rather than, presumably, a dangling field that never gets assigned to

gleaming steeple
#

right, but how do i do that?

rugged oracle
#

Hitbox hitbox = ...GetComponent<Hitbox>()

#

or as aforementioned, similar with TryGetComponent

#

hitbox isn't going to magically refer to the one you want, you have to tell unity what to do

gleaming steeple
#

where am i writing this though

#

in the brackets?

rugged oracle
#

you would do this before the if and then check for hitbox

gleaming steeple
rugged oracle
#

well now you're just getting the Hitbox from the current object instead of from the thing you hit

gleaming steeple
#

forgive me for being obviously stupid but i just wrote exactly what you told me to write?

rugged oracle
#

the ... in my example is omitted code

gleaming steeple
#

i don't understand what you've omitted though

rugged oracle
#

x.GetComponent gets the component from x

#

you put in whatever you want to get the component from

gleaming steeple
#

so what do i actually need to write then? i feel like i'm learning nothing without actually seeing the full solution

rugged oracle
#

rayHit.collider.GetComponent<Hitbox>()

#

have you gone through like, beginner c# courses or are you just winging it

gleaming steeple
rugged oracle
#

not for the Hitbox hitbox = line, no

gleaming steeple
#

like this?

rugged oracle
#

sure

gleaming steeple
#

but mostly winging it

rugged oracle
#

this makes it clear that you're calling hitBox.OnRayCastHit only if it's valid

rugged oracle
# gleaming steeple i've done some beginner ones yes

i feel like this is a pretty simple sequence of logic...

  • x.GetComponent<T>() gets a component T from x
  • you want to get Hitbox from rayHit.collider
    thus, you do rayHit.collider.GetComponent<Hitbox>()
    and youve done this once already, in the if statement
gleaming steeple
#

like this?

rugged oracle
#

yeah

gleaming steeple
#

it's still throwing up the same error, is that because it's not calling the onraycasthit ?

#

it's on that line

rugged oracle
#

is it pointing to the same line, or is it pointing to Hitbox.cs:9

gleaming steeple
#

wtf i just rewrote it and it's working now

#

thanks so much for your help i can imagine it was testing your patience a bit

#

appreciate it

rugged oracle
#

probably didn't save/recompile

gleaming steeple
#

i think i'd used hitBox instead of hitbox

#

lol

#

but thank you