Get Component, change from sprite to game object (Help needed)

I have this code to get a sprite from a scriptable object so I can display it on the floor as an item you can pick up.

//GetComponentInChildren().sprite = item.uiDisplay;
//EditorUtility.SetDirty(GetComponentInChildren());

I would like to change it to a game object instead of a sprite (A 3D model and material in a prefab). How would I modify this code?

Note: uiDisplay = the sprite in the container. I have already changed ‘uiDisplay’ to hold a gameObject instead of a sprite and dragged in the prefab I would like to use.

Thanks

It’s not exactly one-to-one mappable. With a MeshRenderer you need a Mesh (from a MeshFilter), plus use a Material that has a Texture2D. A Sprite is a Texture plus coordinates, and Unity doesn’t let you get the Texture because it might be atlased in some bizarre way, so they don’t want you thinking of it as a Texture2D.

One option is to have a uiDisplayTexture and uiDisplaySprite and deal with them that way.

A more-ideal way is to make prefabs for each item you want, so you swap out the entire prefab, which at that point can be sprite or 3D and the code doesn’t care.

Kurt-Dekker,
I have the same issue with the same code. Could you drop an example?

Here is how it is currently looking like.

Each scriptable object > GameObject is linked to prefab. (Pictures attached)

public abstract class ItemObject : ScriptableObject
{
public int Id;
public Sprite uiDisplay;
public GameObject itemPrefab;

Scriptable object looks like this (key)
7869760--1000015--upload_2022-2-4_20-33-42.png
So, then it can be used as scriptable object below (in the Scene hierarchy)


by using ISerializationCallbackReceiver (Attached script on to item(1))

General idea is to have each scriptable object with its own prefab.
Then when it is changed in Editor > it will automatically change child prefab to what’s selected (For example from “Key” to “watermelon”) > by using OnBeforeSerialize
Any help appreciated :slight_smile:
Thank in
Avance

I’m sorry, I can’t figure out what you are asking for… are you talking about just how to:

because that’s pretty much how every tutorial does anything in Unity… it’s kind of The Unity Way ™ to have separate prefabs and instantiate them.

Kurt thank you for sharing

So how do I swap the entire prefab with the code?

If below code designed to swap sprite only

GetComponentInChildren().sprite = item.uiDisplay;
EditorUtility.SetDirty(GetComponentInChildren());

You understand this doesn’t work at runtime right? This is purely for use within editor because it uses code from the UnityEditor namespace.

Are you writing a custom editor plugin or something that you will be using within the editor while making your game? If you’re writing a custom editor, that’s not really what I’d consider a beginner task.

Answering questions about custom editor functionality really cannot be done in a vacuum. Everything about start / play / stop and serialization is in play when you are writing custom editor swap-out code.

I’m using this for with in scriptable objects. And it works with the sprites

It takes Sprites from Scriptable object (While Scriptable object contains that sprite)

In the editor when I switch my Parent item it automatically switches the sprite (Child).

But since sprite is 2d. It not really working out for my project plan (But it works and displays that sprite in the 3d world)

Main Idea for all of this is basically reduce point of human error.
When changing the item (Parent / In the editor), it automatically should change 3d model (Child / based on scriptable objects DATA), instead of doing this manually every time.

Hope that makes sense

PS: I know I might be taking it too far for a beginner with scripted objects, xexe. But one way or another I will do it =)

Thanks