#so just saying GameObject? or would it

1 messages · Page 1 of 1 (latest)

radiant imp
#

enemyLoop script handles all the list stuff, enemy does nothing but pass itself

#

@oblique hull

oblique hull
#

alright, so then just GameObject?

radiant imp
#

well thats what you declared your list as it seems

#

so yea

oblique hull
#

alright so right now its public void enemydetect(GameObject)

radiant imp
oblique hull
#

yeah i saw that, so i changed it to EnemyDetect(GameObject)

oblique hull
#

its still red right now though

radiant imp
#

did you see the examples on how to declare method parameters?

#

the structure is always the same when you declare in c#

#

type name

oblique hull
#

wait, so what would it need to be in this case? since its not a string for this? GameObject and then a name for it like enemy for example?

radiant imp
#

yea

oblique hull
#

okay i'll do that

#

its still giving a red underline for EnemyDetect so i'm not sure

radiant imp
oblique hull
radiant imp
# oblique hull

well its red because you didnt finish writing it..I don't understand did you not see the examples of how the other methods are written

#

this goes in enemyLoop though

#

or whatever has the list

oblique hull
#

oh i have it

#

i added the brackets okay

#

and i'll move it to the enemyLoop now

#

okay i have it in enemyLoop now

radiant imp
#

write the logic inside, of what you needed earlier

#

remove from list, check list count

oblique hull
#

that would be similar enough from that one thing of code i used right?

radiant imp
#

minus the loop

oblique hull
#

right

radiant imp
#

but RemoveAt works with indexes

#

right now you have enemy

#

which is of type GameObject

oblique hull
#

i see

#

so would i need to make it into an index? or is there something else i can use?

radiant imp
#

yup just use the List other method of removal

oblique hull
#

ah i see

#

would it still be if(enemyList[i] == null) in this case?

#

just wondering since it doesnt have the for loop

radiant imp
#

why would you need that or index at all

#

I mentioned before Lists can shrink

#

or expand, but this case shrink only

oblique hull
#

so i would do something like

#

if(enemy = null)

#

and then i would use the List.Remove?

radiant imp
#

break down each step, do one at time start from top to bottom

#

the top is you are getting enemy from death,

#

now what next

oblique hull
#

time start? huh? im not sure how to go about removing from the list I don't know what i would need happen for that, or well, how to write it i guess?

radiant imp
#

its not even a code thing, you have to like think what you want happening

#

code comes after you logic your way through it

oblique hull
#

at this point we have all of the enemies in the list

radiant imp
#

if you think too much about its a code problem you wont ever solve it.

oblique hull
#

so, i would need to find a way for it to find out whether or not its destroyed?

radiant imp
#

if its coming through that method the assumption is , its already ondeath event

#

think your goal here is your tracking enemies amount..

oblique hull
#

so seeing if one of the enemies doesn't exist anymore right??

#

since when they're destroyed they wouldn't still be on the list?

radiant imp
#

no because destroying a gameobject in the list would only make it null

#

it would still keep its spot in the list

#

as null

#

it would be cumbersome to check the same amount of the list each time if all are null

#

easier to keep track of the Count , simple integer

oblique hull
#

so check if something from the lists count is null

#

and if it is then remove it?

radiant imp
#

so Remove method removes it from list completely

radiant imp
oblique hull
#

or well,

radiant imp
#

its a simple integer

oblique hull
#

would the count go down if something in the list is null?

radiant imp
#

nope

#

thats why an array would not be useful

#

if you had 20 enemies you would have to loop 20 times each time to check if they're all null slots

#

that would be inefficent

oblique hull
#

okay so, what do we need to check then?

radiant imp
#

huh?

#

the same exact thing you were checking earlier..

oblique hull
#

well yeah

radiant imp
#

is none of it making sense at all?

oblique hull
#

i understand that part

#

if its 0 then it does that, i understand that

#

but im not sure how to remove something from the list when it becomes null?

radiant imp
#

and how do you think it would get to 0

oblique hull
radiant imp
oblique hull
#

yes i know, and you showed the remove part, but i dont know what if statement to add for it, i dont know how to say
if a gameobject in the list becomes null, then remove it from the list

#

i understand that once it is empty then i can have if(enemyList.Count == 0 then invoke the function and such

radiant imp
#

enemy parameter is the enemy about to be removed from game

#

the gameObject

#

its one simple line, start there

oblique hull
#

how does it know that it's about to be removed? or is that for the enemyManager part?

#

so i start with enemy inside of the method right

radiant imp
#

enemyManager is irrelevant right now since its empty

oblique hull
#

righrt

radiant imp
oblique hull
#

but i dont know how to start?

radiant imp
#

wdym

#

enemy is your GameObject

#

to be removed from the list

oblique hull
#

like this is what i have right now, i dont know where to start the actual code of it

radiant imp
#

you have the remove it from the list

oblique hull
#

okay, so i just put List.Remove?

radiant imp
#

same as enemyList had method of Count for count

oblique hull
#

so i would put

#

Remove(enemy) or so?

#

well it isnt working i assume?

#

i mean its red

#

the thing you said was List.Remove

radiant imp
#

List has a method called Remove yes

#

just like it has a property for Count

oblique hull
#

but i'm not sure what the <t> part is?

#

so i would need to put

#

List.Remove so far, but im not sure what else i add onto this?

radiant imp
#

List is the Type

#

you already stored that

oblique hull
#

so i would put

#

enemyList.Remove?

radiant imp
#

right

oblique hull
#

ok

#

enemyList.Remove(enemy)?

#

i ahve it in and nothing has red underline

radiant imp
#

it really is this simple

oblique hull
#

so then

#

after that

radiant imp
#

ok so the next part you already have

#

think of it in simple words terms

#

like actions,

oblique hull
#

if(enemyList.Count == 0)
{ the loop function here}

radiant imp
#

enemy removed from list

#

right

oblique hull
#

alright

#

so i'll put that in

#

so i have

#

how exactly does enemy know its the one being destroyed? or is that something else that needs to be added?

#

just added a ;

#

to the enemyList.Remove line

radiant imp
oblique hull
#

in the script for the laser

#

this

#

poorly cropped it

#

but uh

radiant imp
#

enemy needs a script

#

so it can call EnemyDetect

oblique hull
#

i have the enemyManager script now

#

or

#

well here's how i have the enemies set up

radiant imp
#

if you wanna put that as the script go ahead

#

as long as you are aware of what script is what

oblique hull
#

its a bit messy i guess but, i have one object that has all of the prefabs as children of it

#

so i think it would be fine if i put the enemyLoop script into the enemies object? or would i need to put it into the prefab?

radiant imp
#

enemyLoop should be in the scene

oblique hull
#

right now i have enemyLoop on the background, i wasnt sure where else to put it

radiant imp
#

how else did you drag them into the list 🤔

#

did you not prefill the list

oblique hull
#

i did

#

but im thinking instead of having it on the background, i should put it into the overall enemies object, and then enemyManager maybe into the prefab of the enemy? im not sure

#

uhh overall i dont know if it matters exactly where it is, but i have the enemyManager script? so im not sure how to go about having 'enemy' be seen as one of the enemies being destroyed

#

i guess that's the only issue i have right now is that the 'enemy' gameobject, i'm not sure how it is being seen as an enemy that is being destroyed?

radiant imp
#

can you just screenshot the hierarchy i have no idea what you mean by "on the background"

oblique hull
#

oh sorry

#

so i have background, which is just an image on the background of the game
enemies has all of the prefabs loaded in already

#

and i dont have individual names so it probably looks really ugly, but i have the script on the background object, with the list already filled out

radiant imp
#

so put enemyLoop on a gameobject called enemyloop

oblique hull
#

alright, i can do that

#

but the main thing im wondering about is how to have the enemy gameobject be seen as an enemy thats destroyed?

radiant imp
oblique hull
#

but how would i have it know to do it when the laser hits it?

radiant imp
#

because when you Hit it you have that reference

oblique hull
#

and also, so i still have enemyManager, should i put enemyManager on all of the prefabs enemies?

radiant imp
#

enemyManager is irrelevant right now

oblique hull
#

but the enemies need a script dont they?

#

so i can just use that?

radiant imp
#

is that the script you want to use

#

sure

oblique hull
#

okay then, i'll put that into all of the prefabs

#

and then

#

im not sure how to make it be known if the laser has hit them outside of the laser's script?

radiant imp
#

wdym outside the laser script ?

oblique hull
#

i need to have this script in all of the enemies say to remove itself from the list

#

and then destroy itself

#

but it needs something to happen for it to run that

#

which would be having the laser colliding with it?

radiant imp
#

yes the laser calls the whole chain reaction

oblique hull
#

and thats in the laser's script

radiant imp
#

the trigger you showed me yes

#

instead of Destroy method which instantly destroys it

oblique hull
#

how would i go about having that occurrence triggering something happening in the enemies script

radiant imp
#

you just call the method in the trigger

oblique hull
#

wait

radiant imp
#

you would have to get that script from the gameobject

#

with GetComponent

oblique hull
#

okay so

#

in the enemy script

#

i need to GetComponent the laser's script

#

right?

radiant imp
#

otherway around

oblique hull
#

oh

#

okay

#

i need to GetComponent the enemy's script

radiant imp
#

yes

oblique hull
#

and then the enemy's script has the method for removing the enemy from the list and destroying it

#

and i need to call that method instead of just destroying it right

radiant imp
#

right

oblique hull
#

okay let me try and set that up then

radiant imp
#

the removing from list part you already did inside enemyLoop

#

just need to call that from enemy Script

oblique hull
#

wait huh

#

so i need to

#

wait

#

so laserscript needs to getcomponent the enemy's script

radiant imp
#

as i said earlier, all ur doing is references and calling methods back n forth

radiant imp
oblique hull
#

enemy's script would need to getcomponent the enemyLoop script

#

and then the enemy's script would call the EnemyDetect method, and then destroy itself

radiant imp
#

yeah but GetComponent only works on the gameobject itself or another gameobject you already have refernece to

oblique hull
#

wdym? uhh

#

well the laser prefab has the laser's script

#

and then the empty object for the loop has enemyLoop right now

radiant imp
#

laser already has enemy's gameObject

#

so you can do GetComponent on it easily

#

enemy script needs to store enemLoop inside a variable

oblique hull
#

well it has the collision.gameObject

radiant imp
#

right thats the gameObject which can do GetComponent<T>

oblique hull
#

im not sure how to write that out in this though? im not sure where to put it either

oblique hull
#

i need to sleep soon im just trying to get this figured out its already 4am uhhh

#

so would it be

#

GetComponent<enemyLoop>() ?

radiant imp
#

wait you're starting from the beginning which is the laser

oblique hull
#

i dont know what i need to connect with what

#

on the laser script

#

i need to connect it to the enemy's script right?

radiant imp
#

laser -> enemy -> enemyLoop

#

so collision.gameObject is your starting point

#

from the laser

oblique hull
#

right

#

so where should i put the GetComponent part here?

#

should i just put it right there? or earlier in the code?

radiant imp
#

GetComponent from the object you hit

oblique hull
#

and i would put GetComponent<enemyManager>() ?

#

in the if statement for the collision

radiant imp
#

you can directly access the method

oblique hull
#

how?

#

or wait

radiant imp
#

. operator

oblique hull
#

would it be GetComponent<enemyManager>(method here?)

#

or

#

oh

#

GetComponent<enemyManager>() . methodname

radiant imp
#

right

#

but GetComponent on the enemy's gameobject

oblique hull
#

ok so

#

what does that mean?

#

where would i put that?

radiant imp
#

if you just write GetComponent it will try to look for it on the laser script

oblique hull
#

or how would i write that?

#

how do i make it look for the enemy's gameobject?

radiant imp
#

do getcomponent on the enemys gameobject

oblique hull
#

how do i do that?

#

do i write that in the laser script?

radiant imp
oblique hull
#

okay

#

collision.gameObject and then where do i put the getcomponent?

#

do i attach it onto that?

#

i dont really understand how to write that sort of stuff

radiant imp
#

its all a bunch of .

#

its not that hard

#

think like a big chain

#

of pieces of legos that fit

oblique hull
#

i mean its probably not but i have no idea what . is, we didnt learn any of this coding stuff in that class

#

so its like

#

collision.gameObject . GetComponent<enemyManager>() . methodname??

radiant imp
#

pretty much

oblique hull
#

okay so i'll put that

radiant imp
#

except you have to make that methodname ofc or it wont exist

oblique hull
#

in place of destroying the collision

#

right

#

so im going to make a method in the enemyManager script now

#

and i'll just call it DeleteEnemy

radiant imp
#

yea

#

inside enemy script you only need reference to enemyLoop

#

store it in a variable same as usual

oblique hull
#

wait

#

im not sure though

#

this isnt working

#

its all red underline

radiant imp
#

show

oblique hull
#

oh wait

#

i know whats wrong i need to put the script onto the prefab now

radiant imp
#

no

#

scripts doesnt know anything about prefabs

#

show whats red

oblique hull
#

the entire line

radiant imp
#

why are you putting whitespaces 🤔

oblique hull
#

i fixed it

#

i wasnt sure if i needed to or not

radiant imp
#

also DeleteEnemy is a method

oblique hull
#

yes?

radiant imp
#

methods have () 's

oblique hull
#

oh right

#

okay

#

now the only issue is the enemyManager part

#

oh

#

wait

#

i know whats wrong with it

#

okay we're good now

#

i had changed the name earlier

#

and the class and stuff

#

and i put Manager

#

when its now enemyScript

#

okay

#

that all works

#

so now

radiant imp
#

as long as filenames match classes

oblique hull
#

it does

#

so now in enemyScript

#

i need to

radiant imp
#

all its left is a reference from enemy to enemyLoop

oblique hull
#

how do i go about doing that?

#

its still a GetComponent?

#

or no?

radiant imp
#

since you dont have the gameobject directly no

#

then call it in awake

#

the second example is a bit better..

oblique hull
#

i only see one example?

#

oh

#

i see what you mean

#

the second part of it

radiant imp
#

yea..

oblique hull
#

so i would need to

#

enemy = FindFirstObjectByType<>()?

#

but im not sure

radiant imp
#

just don't store it in a local variable, it must be a class level variable

oblique hull
#

huh?

radiant imp
#

first declare the variable

oblique hull
#

oh

#

right so i'd put that outside of any methods

#

just in the class

radiant imp
#

yes

oblique hull
#

okay

radiant imp
#

not the method just the declaring of the reference

oblique hull
#

so would it be called enemy?

#

or something else

radiant imp
#

name it somethin that makes sense lol

oblique hull
#

alright uhh

#

deletedEnemy or so?

#

idk

#

since this is processing that it is deleted

radiant imp
#

these are horrid names, they should just be what they are

#

the type is enemyLoop

oblique hull
#

type?

#

so lets say i go with deletedEnemy idk

#

i would put

#

deletedEnemy = FindFirstObjectByType<enemyLoop>() ?

radiant imp
#

but did you declare deletedEnemy first

oblique hull
#

would it be declared a gameobject?

radiant imp
#

no

oblique hull
#

what would it be declared as?

radiant imp
#

its type

#

the class is the type

oblique hull
#

so like

#

public enemyScript deletedEnemy

#

or something like that?

#

and then

oblique hull
#

is that how it would work?

radiant imp
#

wdym how would it work

oblique hull
#

is that how it should be

radiant imp
#

pretty much

oblique hull
#

public enemyScript deletedEnemy

#

okay

#

so i'll put that in

#

wait

#

now it has red underlines

radiant imp
#

because i said that part goes in awake?

oblique hull
#

okay

#

so put it in start?

oblique hull
#

do i need to make that?

#

okay

#

i'll do that

#

now its saying it cant convert enemyLoop to enemyScript?

radiant imp
#

which version of unity are you on?

#

also you forgot ;

oblique hull
#

2021.3.34f1

#

i put ; now too uhh

radiant imp
#

oh

#

same exact thing, slightly different wording

oblique hull
#

it still says the same thing?

radiant imp
#

because deletedEnemy is the wrong type

#

you wrote enemyScript

oblique hull
#

oh

#

okay its fine now

#

so then

#

now i just write the deleteEnemy method right?

radiant imp
#

enemyDetect

#

you have to call EnemyDetect

oblique hull
#

oh

radiant imp
#

but yeah you need it inside delete method

oblique hull
#

right oaky

radiant imp
#

it should pretty much be working after that

oblique hull
#

how should i call it?

#

would i just put uhh

#

enemyloop.EnemyDetect ?

#

do i need to declare enemy in this?

radiant imp
#

pretty much

oblique hull
#

because id need to have it be

radiant imp
#

pass itself

oblique hull
#

enemyloop.EnemyDetect(enemy)

radiant imp
#

what is enemy

oblique hull
#

which i tried but it said enemy didnt exist

#

its the gameobject in EnemyDetect ?

radiant imp
#

it expect gameobject

#

so pass it gameobject

oblique hull
#

enemyloop.EnemyDetect(gameObject enemy)

#

like that?

#

or

radiant imp
#

what is enemy

oblique hull
#

the gameobject in EnemyDetect isnt it?

#

wdym by that

radiant imp
#

you don't need variable names when passing parameters..

oblique hull
#

so when what would i put?

#

putting enemy by itself doesnt work because it doesnt exist

radiant imp
#

the only thing the underline is complaining about

oblique hull
#

so do i need to declare enemy?

radiant imp
#

gameObject is the enemy

oblique hull
#

so i would put gameObject instead

radiant imp
#

ye

oblique hull
#

but now it says

radiant imp
#

what does error say

oblique hull
#

object reference is required for non-static field, method, or property

radiant imp
#

is enemyloop not the variable ?

#

what did you do

oblique hull
#

which variable???

#

enemyloop is the script

radiant imp
#

ugh don't use lowercase for classes

#

shits confusing as hell

#

classes should be capitalized

oblique hull
#

yes i realized that but this is what i have in that

#

so im not sure what variable you mean?

radiant imp
#

what did you name the class..

oblique hull
#

enemyloop

#

its the same as the script's filename

radiant imp
#

the method is irrelevant right now

#

then you should not be using the class directly in enemy script..

oblique hull
#

then what do i put there?

radiant imp
#

as i already said, store it in a variable..

oblique hull
#

store what in a variable??

radiant imp
#

wdym store what

oblique hull
#

you said store 'it' in a variable

#

what is 'it'

radiant imp
#

enemyLoop

oblique hull
#

enemyLoop is the script

radiant imp
#

you named it enemyloop

oblique hull
radiant imp
#

why did you change it

oblique hull
#

enemyloop is the script

radiant imp
#

from enemyLoop

oblique hull
#

i didnt change it

#

its always been enemyloop

#

the filename is enemyloop, the class name is enemyloop

#

always has been

oblique hull
radiant imp
#

ugh alr . always capitalize your classes/filename then

#

enemyloop. store it in a variable

oblique hull
#

wdym by that

#

is that not what i did by saying

radiant imp
#

you called deletedEnemy ??

oblique hull
#

no?

radiant imp
#

why are you using enemyloop.EnemyDetect then

oblique hull
#

thats what i had said before i dont know

#

wdym called?

#

wait

#

yeah? i dont know

#

so i need to put

radiant imp
#

how do you not know

oblique hull
#

i showed you the code

radiant imp
#

the whole point of doing this

#

is to use enemyloop through deletedEnemy

#

if thats where you're putting the reference

oblique hull
#

im barely awake right now im just tryin gto get this last bit of code through

#

so should it be

#

deletedEnemy.EnemyDetect?

#

since thats the varaible i have with the enemyloop find object of type?

#

ok there it goes

#

so that calls enemy detect

#

so now i just need to put destroy gameobject right?

radiant imp
#

pretty much

oblique hull
#

looks fine now so

#

it should be fine to test yes?

radiant imp
#

should be

oblique hull
#

ok

#

it works

radiant imp
#

you can check inspector as well if the list shrinks

oblique hull
#

we're good then now

#

it restarted and everything

radiant imp
#

yes its very simple stuff to do

oblique hull
#

when you understand this stuff

radiant imp
#

well the main thing to learn is the basics

oblique hull
#

maybe if my class actually taught coding instead of just the engine it'd be nice but hmm

#

but its fine now

radiant imp
#

coding is important too , but logic is far more useful

#

try to break down into smaller easier to find bits of problems

oblique hull
#

im just not very smart with this sort of stuff, kinda realized that way earlier this semester loll but ive made it through the class so far

#

but thank you

radiant imp
#

you can see each step led to this result

#

np

oblique hull
#

yeah

radiant imp
#

once you get down references, it should become easier with time

#

goodluk UnityChanSalute