playMaker

Author Topic: Remembering object in a prefab  (Read 723 times)

Krauser1129

  • Playmaker Newbie
  • *
  • Posts: 22
Remembering object in a prefab
« on: September 13, 2020, 10:26:38 AM »
I need a spawned enemy to remember to use the main camera for its canvas. Can't seem to do it. Even have the camera saved as variable MainCameraV but the camera slot is in the inspector so I don't know what to do.

Thore

  • Sr. Member
  • ****
  • Posts: 480
Re: Remembering object in a prefab
« Reply #1 on: September 13, 2020, 11:16:21 AM »
In Unity, prefabs can’t keep reference with scene objects, only within their own local hierarchy. To create the reference (“remembering”) at runtime, you need to find that object with some other means. The easy but potentially less robust solution is finding by tag (or name, but avoid using name, because you might rename later). Finding by tag is perfectly fine, but not ideal if this issue comes up thousands of times.

The more elaborate and robust solutions are the domain of controllers or managers, two different styles to manage objects. These are dedicated objects that keep track of objects and references.

ch1ky3n

  • Full Member
  • ***
  • Posts: 208
Re: Remembering object in a prefab
« Reply #2 on: September 14, 2020, 12:01:36 AM »
Not sure of what you're trying to achieve.

beside Thore solution,
you can do below's solution as well,

1. The easiest is to set a Global Variable of Game Object. Name it to " MY CAMERA" and assign your camera on it at your initial script. Thus by giving it as a global variable, means you can now refer it from the prefab.

2. Send event + Set Event property

Instantiate prefab
set property : Game Object = Main Camera
Send event "I WANT YOU TO FOLLOW MAIN CAMERA"

On the prefab make a global Event  "I WANT YOU TO FOLLOW MAIN CAMERA"
inside the state
get property : Game Object = Main Camera


There you have it.

« Last Edit: September 14, 2020, 12:03:28 AM by ch1ky3n »