#enemS is the reference to the script on
1 messages · Page 1 of 1 (latest)
thats what trygetcomponent does
so it will create a new variable in the enemy script?
no
that local variable is what your player will know the enemy it hit as , the script its own
enemyS = enemyScript?
it only names it when it hits
because you dont need to write if(enemyScript == null) do this
trygetcomponent does the null check for you, so in your case if hits something that has enemyScript component on it, it gets it and names it enemyS and then uses it to grab whatever variable / methods u need
um u should have intellisense working
.TryGetComponent is named with pascal case
you obviously called it EnemiesInGeneral
dont just copy without thinking through
it's mostly pseudo code
to make u understand
ur blindly copying ur not gonna understand
ah ok
do this first
show me it's working
then i can help you
no i told you, it's a local variable
it's only set by your player on collision
it only exists within that method of collision
it creates the reference to the script component you are doing with trygetcomponent
right
whats the diff between doing it in start void and doing it there?
or is it just the same?
because on start it's wrong you're trying to GetComponent on the player
oooh on the player, but its on the enemy
GetComponent works on the script it's calling it unless you assign it a different reference
like somethingElseAlreadyReferenced.GetComponent
so TryGetComponent would get it outside the script its calling?
no collision.
is referencing to the collider of your oncollision
so trygetcomponent or getcomponent would be called from the collision. in this case gameObject
collision. part is whats telling it to look at the collider we hit and no trygetcomponent on same script it got called from
in this case the gameobject which hits the collider
yea
the collider of the gameobject we hit
because the player is calling OnCollision
OnCollision is just a method
"and no trygetcomponent on same script it got called from
"?
i meant Not, it's not trying to call trygetcomponent on itself
because of the .collision
you can use trygetcomponent the same way you use getcomponent it does that, gets a component
?
ex: its on player script
so if player got hit by ball
it doesnt call it on player because that's itself
but on the ball
right?
what does
?
idk what you mean
like it doesnt call the component
on itself
but on the ball
because thats the other collider?
you see oncollision method?
yes
it takes in a (Collider collision)
so it needs a collider, which it gets from the physics that occurs with the rigidbody
yeaaa
that collision parameter becomes the other referenced collider
yeah exactly
ok so far, so good.
so if the player got hit by something that got enemyScript on it, the player script will get not on itself, but the component on the other script which is the enemy script
right
it will try to get whatever component u tell it to get on collider it hit
only because you already have that info via collider parameter from OnCollision
you can also try get a different component on player if it has a separate script like stamina lets say
fix up the script and show what u have now
u can remove the &&tag
why
whats "pjt" anyway ?
short for projectile
so obviously it wouldn't be hit by a bullet and enemy collider at the same time
thats not gonna work
but why would it be hit by a enemy collider?
i think ur confused on how references even work
y would u reference damage on enemy if its oncollision w bullet
because i instantiated the bullet on that script
hows the bullet know where it came from?
i spawned it
things dont happen by magic in code
wait i confused myself
either put damage amount on the bullet itself or pass the damage amount to the bullet from the enemy
prob cuz ur doing this whole thing backwards and you dont seem to know urself what you want to do
you mentioned nothing about a bullet this whole time , start with that
ok but the projectile has no info about damage
my suggestions is to start with some c# basic or refresh because you're missing some key concepts here
ok yeah good idea, its been like months since i last coded and past me was working on this
Rigidbody rb = Instantiate(projectile, spawnP.position, Quaternion.identity).GetComponent<Rigidbody>();
rb.AddForce(transform.forward * 32f, ForceMode.Impulse);```
your enemy is already creating the bullet there with a reference to it, just make a script that holds a damage value you can pass it in this attack method when you instantiate it
why would i make another script just for that?
how else would ur bullet hold a damage value ?
ok. entertain me. show me how that would work
ok
using UnityEngine.UI;
using UnityEngine;
public class pjtEnemy : MonoBehaviour
{
public GameObject bullet;
public float dmgOfBullet;
playerGeneral hpS;
void Start()
{
}
private void OnCollisionEnter(Collision collision)
{
if (collision.collider.CompareTag("pjt"))
{
hpS.hp -= dmgOfBullet;
}
}
}```
does this make sense
is this right?