Adam's Lair Forum
https://forum.adamslair.net:80/

How to find/get prefab of an object?
https://forum.adamslair.net:80/viewtopic.php?f=4&t=1248
Page 1 of 1

Author:  ddabrahim [ 2018/09/05, 13:35 ]
Post subject:  How to find/get prefab of an object?

Hi.

I'm trying to create some objects in the level during runtime but instead of having a public property to set prefab manually in the editor, I would like that if my component could figure this out and find the prefab of the object on it own. But I can't find the solution.

This is what I tried:

Code:
ContentRef<Prefab> objPrefab = this.GameObj.PrefabLink.Prefab;


But when I try to run a preview I get Null Reference Exception error saying Object reference not set to an instance of an object and PrefabLink returned null. Which basically means it did not find a link to a prefab I guess, but I'm not sure why. The object does have a prefab.
Could someone explain please why it doesn't work?

I can get it to work if I create a public property and set the prefab manually in the editor, but I though it would be fun to be able to make the component figure this out on it own.

I would appreciate any help.
Thanks.

Author:  SirePi [ 2018/09/05, 14:20 ]
Post subject:  Re: How to find/get prefab of an object?

AFAIK, the PrefabLink is a property that is only available in the editor, and gets discarded when running the game proper - as you can notice, while the editor is in "Editor" mode, the SceneView displays your prefab object in blue, and with a link icon, while it appears black as all the others when running in "Game" mode; so you can't rely on it to do what you want to.

This said, you can either associate a ContentRef<Prefab> property to your Component (as you already did), or simply do a manual lookup in the ContentProvider (either you know the name of the Prefab resource, or you do some logic of your own to find it).

Something like this:
Code:
ContentRef<Prefab> pf = ContentProvider.RequestContent<Prefab>(@"Data\MyPrefab.Prefab.res");
if (pf.IsAvailable)
   this.GameObj.ParentScene.AddObject(pf.Res.Instantiate());

Hope it helps :D

Author:  ddabrahim [ 2018/09/05, 16:21 ]
Post subject:  Re: How to find/get prefab of an object?

Thank you for the explanation. It helped a lot :)

It explains then why I was able to totally unintentionally create an editor plugin... well sort of. It was more of a bug than a plugin xD

I managed to get it working in the preview, but then something probably crashed because even when I stopped the preview, the scene did not reset and all the objects created in the preview remained in the editor and I was keep getting the message the scene was edited externally. I though at first it cool but then realized something seriously messed up because the game did not run any more with the Launcher not even if I changed everything back as it were and after messing around a bit more I was unable to load the scene any more. Fortunately I had a backup.

I should have triggered something that Dualitor was not prepared for :twisted:

Page 1 of 1 All times are UTC + 1 hour [ DST ]
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/