#Right now you're trying to get the
1 messages · Page 1 of 1 (latest)
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
If you want to get the entity from the thing you hit, why not get the component from the thing you hit
writing TryGetComponent, or any method really, on its own results in this.TryGetComponent
Instead of this
You already know how to get a component from the thing you hit, you're getting the hitbox
i don't really know how to answer it, i thought i did. i'm trying to check if the ray hit the hitbox of the enemy
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
it gets added to every collider when the enemy is spawned in
well every limb
...so that has an OnRayCastHit that's calling Entity.TakeDamage, why aren't you using that
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
so Hitbox.OnRayCastHit instead?
that doesn't do what i said, no
i don't know what you mean by use the hitbox
use the one you get from GetComponent
GetComponent returns a value
surely you've used GetComponent before?
i've used getcomponent before but i'm not sure what you mean
ComponentType componentInstance = targetGameObject.GetComponent<ComponentType>()
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
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
right, but how do i do that?
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
you would do this before the if and then check for hitbox
well now you're just getting the Hitbox from the current object instead of from the thing you hit
forgive me for being obviously stupid but i just wrote exactly what you told me to write?
the ... in my example is omitted code
i don't understand what you've omitted though
x.GetComponent gets the component from x
you put in whatever you want to get the component from
so what do i actually need to write then? i feel like i'm learning nothing without actually seeing the full solution
rayHit.collider.GetComponent<Hitbox>()
have you gone through like, beginner c# courses or are you just winging it
is that not exactly what i've wrote there?
not for the Hitbox hitbox = line, no
like this?
sure
i've done some beginner ones yes
but mostly winging it
and then notice you have duplicated code, so you can remove the second GetComponent call by just checking hitbox
this makes it clear that you're calling hitBox.OnRayCastHit only if it's valid
i feel like this is a pretty simple sequence of logic...
x.GetComponent<T>()gets a componentTfromx- you want to get
HitboxfromrayHit.collider
thus, you dorayHit.collider.GetComponent<Hitbox>()
and youve done this once already, in theifstatement
like this?
yeah
it's still throwing up the same error, is that because it's not calling the onraycasthit ?
it's on that line
is it pointing to the same line, or is it pointing to Hitbox.cs:9
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
probably didn't save/recompile