playMaker

Author Topic: Best way to get a child from gameobject variable  (Read 2312 times)

Mupp

  • Full Member
  • ***
  • Posts: 167
Best way to get a child from gameobject variable
« on: February 21, 2021, 07:59:51 PM »
I have prefabs which contains lots of children and a few child's to some children. If I store one of those prefabs in a variable, what is the best way to get a known child in it?

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7618
    • jinxtergames
Re: Best way to get a child from gameobject variable
« Reply #1 on: February 22, 2021, 08:55:55 AM »
Hi.
Usually i use a fsm (and i call it 'Data')
on the root of the prefab.

Then i set any kind of variables on it.

For example child gameobjects would be in a named variable or array/hashtable (depending on your need)

Other example : Data could hold current/max health from the enemy and on the enemy damage fsm I would use get/set fsm actions (and other fsm action like fsm float add / fsm float compare / etc. (Ecosystem))

When the prefab is spawned you can store it somewhere to access if needed.


Mupp

  • Full Member
  • ***
  • Posts: 167
Re: Best way to get a child from gameobject variable
« Reply #2 on: February 22, 2021, 09:32:50 AM »
Thanks. But say if you have lots of prefabs(hundreds or thousands). Can you do it through a template?

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7618
    • jinxtergames
Re: Best way to get a child from gameobject variable
« Reply #3 on: February 22, 2021, 05:12:40 PM »
Hi.
you mean to add the child's in an array @ runtime?
Maybe you can explain more on your use case :)

Mupp

  • Full Member
  • ***
  • Posts: 167
Re: Best way to get a child from gameobject variable
« Reply #4 on: February 22, 2021, 07:32:15 PM »
Not really, I just want to move around some specific children from a spawned prefab. But to do that I need a way to point to them. But to manually do that for hundreds or more prefabs is not that appealing, so I wonder if it's possible to do this with a generic template.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7618
    • jinxtergames
Re: Best way to get a child from gameobject variable
« Reply #5 on: February 23, 2021, 07:08:28 AM »
Hi.
Maybe Best to use array maker,
you can preset an array list component or create at runtime (Array List Create)
1st do 'Array List Get All Child Of Game Object'

Now you have all objects in a list.

Next Step you can Loop Thru the array by using Array List Get Next.

going thru the list you could group them into different arrays or hash tables.

for example you could get the childs tags and group them by tag or if all child's have unique names you could build a Hash table with it.

for hash table, you can also create @ runtime.

When you want to group by tag you can get tag, then have a checker if tag has already a hashtable/array list. if not create a hashtable/array list amd add it to a array so you can check if it was created.
to check you can use array(list)contains with the tag string.

Now you can access the arrays/hashtables by using the reference.

here are some tutorials that uses array maker :




Mupp

  • Full Member
  • ***
  • Posts: 167
Re: Best way to get a child from gameobject variable
« Reply #6 on: February 23, 2021, 10:21:07 AM »
Thanks, that's a lot to take in, but all this is to avoid using find gameobject?

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7618
    • jinxtergames
Re: Best way to get a child from gameobject variable
« Reply #7 on: February 23, 2021, 06:47:56 PM »
HI.
if you only use find gameobject once in a while it should be fine.
Best is to try it in a build.

if you need to do al lot of find gameobjects in the same frame then it might be an issue.

find gameobjects will go thru all objects in your scene, so the more object the more it could go thru to find it and then multiplied by several 'find gameobject'

it could become an issue.

Mupp

  • Full Member
  • ***
  • Posts: 167
Re: Best way to get a child from gameobject variable
« Reply #8 on: February 23, 2021, 07:20:47 PM »
Ok, I see. Thanks.