playMaker

Author Topic: How do I reference prefabs as game objects from instances?[SOLVED]  (Read 2724 times)

CADR

  • Playmaker Newbie
  • *
  • Posts: 1
I am trying to make a functional spawning system for a game that I am making. Everything works fine by itself, but it doesn't mix well together. I know what the problem is and I've spent the past two days trying to fix it, but nothing has worked. When I create instances of the prefabs none of the referenced game objects are what I want them to be. I've tried to use global variables, but nothing I did worked. If this is the best solution and I'm just doing it wrong please help. If it any help to you, the enemy AI in my game was base, in majority, from the Basic AI System tutorial by BadSeedGames. I really need some help, I cant do anything else until I get this working.
« Last Edit: March 15, 2017, 04:26:33 AM by jeanfabre »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: How do I reference prefabs as game objects from instances?
« Reply #1 on: January 02, 2014, 06:03:29 AM »
Hi,

Yes, it's a unity restrictions and the way to go is the following:

-- In the logic that is used to spawn a prefab, keep a reference of the prefab you spawned and send it a message with a reference of the gameobject you want that prefab to be aware of.

-- the spawned prefab could "find" an object when it starts ( be careful, not really optimized if you search by name but finding a gameobject by tag is ok).

Bye,

 Jean

krane

  • Junior Playmaker
  • **
  • Posts: 63
Re: How do I reference prefabs as game objects from instances?
« Reply #2 on: March 14, 2017, 02:08:32 PM »
Jean (or someone else) can you explain this answer?  I'm confused about instances of prefabs and how to refer to the instance, not the prefab. thanks

Ofonna

  • Full Member
  • ***
  • Posts: 230
Re: How do I reference prefabs as game objects from instances?
« Reply #3 on: March 14, 2017, 03:08:54 PM »
whenever you use the "create object action" theres always an option to store the object in a variable, you can work with that.

you can check out my little game and see how i was able to work with spawned platforms, changing its position and material

https://play.google.com/store/apps/details?id=com.play24ever.JumpAlfiejump
« Last Edit: March 14, 2017, 03:12:27 PM by Carmichael »

krane

  • Junior Playmaker
  • **
  • Posts: 63
Re: How do I reference prefabs as game objects from instances? [SOLVED]
« Reply #4 on: March 14, 2017, 05:47:38 PM »
Thanks Carmichael. I don't have Android, so can't see your game, but thanks.

I've got a bullet prefab (tagged Bullet) being shot at an enemy prefab (tagged Enemy), and it seems no matter what I do, I can't destroy the enemy.

The enemy spawner creates the enemy and stores it as EnemyGO. The bullet spawner creates the bullet and stores it as BulletGO.

When the enemy collides with the bullet, the collision event should destroy both (using destroy self on each prefab), but neither get destroyed, although the event is triggered in the FSM.   

Note: if I drag and drop a prefab bullet to the middle of the scene, that one bullet will kill an enemy, but any spawned bullets do not--they just bounce off. 

Is this to do with prefabs, or another problem?

(later update:)
Ah jeez, it was because I hadn't applied some changes to my prefab. doh.
« Last Edit: March 14, 2017, 06:17:48 PM by krane »

Ofonna

  • Full Member
  • ***
  • Posts: 230
Re: How do I reference prefabs as game objects from instances?
« Reply #5 on: March 14, 2017, 10:51:08 PM »
lol glad you were able to figure it out, little things like this come up all the time