#enemS is the reference to the script on

1 messages · Page 1 of 1 (latest)

severe sinew
#

but i dont have a local variable name for enemyS?

full path
#

thats what trygetcomponent does

severe sinew
#

so it will create a new variable in the enemy script?

full path
#

no

#

that local variable is what your player will know the enemy it hit as , the script its own

severe sinew
#

enemyS = enemyScript?

full path
#

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

severe sinew
#

?

#

i am still confused about it

#

it names what?

full path
#

.TryGetComponent is named with pascal case

severe sinew
full path
#

you should get autosuggest working

#

^

#

^

#

^

#

obviously gameobject isn't a thing

severe sinew
full path
#

you obviously called it EnemiesInGeneral

severe sinew
#

thats what you mean

#

oooh

full path
#

dont just copy without thinking through

#

it's mostly pseudo code

#

to make u understand

#

ur blindly copying ur not gonna understand

severe sinew
#

ah ok

full path
#

fix intellisense

#

it wouldve told you

severe sinew
#

so then it would return it as enemyS right

#

for the collison?

full path
#

do this first

#

show me it's working

#

then i can help you

severe sinew
#

ok

#

ok i think it works

#

so now for the enemyS

full path
#

now you are more guided

#

and not spell error

severe sinew
#

do i need to create it in the enemy script?

#

as a variable?

full path
#

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

severe sinew
#

ah ok

#

but what does it do?

full path
#

it creates the reference to the script component you are doing with trygetcomponent

severe sinew
#

oooh instead of doing it in the start void

#

ok

full path
#

right

severe sinew
#

whats the diff between doing it in start void and doing it there?

#

or is it just the same?

full path
severe sinew
#

oooh on the player, but its on the enemy

full path
#

GetComponent works on the script it's calling it unless you assign it a different reference

#

like somethingElseAlreadyReferenced.GetComponent

severe sinew
#

so TryGetComponent would get it outside the script its calling?

full path
#

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

severe sinew
#

in this case the gameobject which hits the collider

full path
#

yea

#

the collider of the gameobject we hit

#

because the player is calling OnCollision

#

OnCollision is just a method

severe sinew
full path
#

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

severe sinew
#

it is trying to find it on the other script

#

that hit on the one itself

full path
#

?

severe sinew
#

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?

full path
#

what does

severe sinew
#

?

full path
#

idk what you mean

severe sinew
#

like it doesnt call the component

#

on itself

#

but on the ball

#

because thats the other collider?

full path
#

you see oncollision method?

severe sinew
#

yes

full path
#

it takes in a (Collider collision)

#

so it needs a collider, which it gets from the physics that occurs with the rigidbody

severe sinew
#

yeaaa

full path
#

that collision parameter becomes the other referenced collider

severe sinew
#

yeah exactly

full path
#

ok so far, so good.

severe sinew
#

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

full path
#

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

severe sinew
#

i did this but it doesnt show the debug when the player gets hit by the object

#

?

severe sinew
#

why

full path
#

whats "pjt" anyway ?

severe sinew
#

short for projectile

full path
#

so obviously it wouldn't be hit by a bullet and enemy collider at the same time

#

thats not gonna work

severe sinew
#

but why would it be hit by a enemy collider?

full path
#

you sad hit by enemy damage player

#

now ur sayin bullet

severe sinew
#

enemy damages player by a bullet

#

you've got me confused now

full path
#

y would u reference damage on enemy if its oncollision w bullet

severe sinew
#

because i instantiated the bullet on that script

full path
#

hows the bullet know where it came from?

severe sinew
#

i spawned it

full path
#

things dont happen by magic in code

severe sinew
#

wait i confused myself

full path
#

either put damage amount on the bullet itself or pass the damage amount to the bullet from the enemy

full path
#

you mentioned nothing about a bullet this whole time , start with that

severe sinew
#

ok

#

i think i used wrong script

#

by accident

#

this is the correct script^

full path
#

ok but the projectile has no info about damage

severe sinew
#

not sure how to do that

#

maybe through if statement?

full path
#

my suggestions is to start with some c# basic or refresh because you're missing some key concepts here

severe sinew
#

ok yeah good idea, its been like months since i last coded and past me was working on this

full path
#
            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

severe sinew
#

why would i make another script just for that?

full path
severe sinew
#

then i'll make a if statement?

#

plus a local variable?

full path
severe sinew
#

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;
        }
    }


}```
severe sinew
severe sinew
#

is this right?