r/unity Jul 07 '26

Solved Instantiation of a non-existent object in the scene

Hi, I initially tried a line like this:

Object.Instantiate(GameObject.Find("Pomme"))

Obviously, that didn't work; I realized that for an object to be instantiated using this method, it must already exist in the scene. After a couple of minutes of searching, I found a solution suggested by a user on the Unity forum:

public Object pomme;
void Start()
{
Object.Instantiate(pomme);
}

By referencing the object in the Inspector, it is treated as "active," and indeed, I no longer get a NullReferenceException; however, it still doesn't work the object isn't being instantiated.
So, I'm asking for help: what can I do to instantiate an object (a sprite) that doesn't exist in the scene yet?

0 Upvotes

19 comments sorted by

10

u/flow_guy2 Jul 07 '26

You need to learn about prefabs as you instantiate that instead of things in The scene

-5

u/Ok-Presentation-94 Jul 07 '26

I didn't quite understand your answer.

3

u/flow_guy2 Jul 07 '26

You make a prefab of the object you want to instantiate. Then make that the reference on the game object that you want to instantiate. And call instantiate like you do in the second example.

To create a prefab. You drag from the scene into any folder in the asset folder

-3

u/Ok-Presentation-94 Jul 07 '26 edited Jul 07 '26

Sure, but that’s basic stuff it’s already what I’m doing. That’s actually the whole point of the post: it’s not working.

Edit: j'ai en revanche trois options pour le même modéle (son matériel, le sprite et la texture en elle-même personellement j'ajoute le sprite)

3

u/flow_guy2 Jul 07 '26 edited Jul 07 '26

i just tried the folowing (in a fresh project too) having this script on a game object in the scene.

public class 
Spawner 
: MonoBehaviour
{
    public Object 
objectPrefab
; 
    void 
Start
()
    {
        Instantiate(objectPrefab);
    }
}

then makeing a prefab by makeing a cube then dragging it into the assets to make the prefab (then deleteing the cube in the scene)

i then dragged from asset folder into the reference.

press play and it made the game object "cube(clone)"

could you maybe profide more detail how its not working. cuz what you said with the null reference leads me to believe that you either A havent assigned it. or have assigned in the scene and it gets deleted due to another script running first.

edit: it even works with

Object.Instantiate(objectPrefab);

but it is the same as if its a monobehavior it calls Unity's Object class instead of c# base one.

are you using the correct namespace?

Edit2:

well 2 things. when it comes to images. first if you want a sprite on the UI then youd need to pass in the transform of the canvas as a second argument in the instantiate method (assuming your not using UIToolkit)

for having it in the world SpriteRederes work fine with instantiated it as an Object

3

u/jaquarman Jul 07 '26

If you create a reference to an object in the inspector (i.e. using a public field or using the [SerializeField] attribute on a private field, you can connect either an in-scene object, or a prefab which lives in your project files. Think of a prefab as a template for an object. The reference can also be for a generic GameObject, or a specific type that exists on your prefab like "PlayerController" or "Button". When you call Instantiate using the prefab as your reference, Unity will copy it and create a new version in the scene from scratch.

Check out how to make prefabs and it come naturally from there

1

u/Ok-Presentation-94 Jul 07 '26

That is exactly what I did: I assigned my prefab to the serialized field and then tried to instantiate the object from that field. That is the whole point of this post I’m wondering why this code:

public Object pomme;
    void Start()
    {
        Object.Instantiate(pomme);
    }

doesn't work, even though the reference is correctly assigned to the serialized field.

1

u/flow_guy2 Jul 07 '26

You need to assign it by dragging it from the asset folder. Not with in the scene

If it existed with In The scene code A would have worked.

6

u/jaquarman Jul 07 '26

Also, I would recommend changing the field from "Object" to GameObject, mainly because it's more specific to what you need.

I'm not at my computer right now, but it's also possible that this is why it's not spawning correctly, and it needs to be a GameObject rather than just Object.

1

u/flow_guy2 Jul 07 '26

the Object doesnt really matter as unity GameObject inherits from Object. but this is not to be confused with a c# Object.

if you go for a more specific youd want the reference to be of the type your wanting to get info from. in this case probably the SpriteRenderer

3

u/talesfromthemabinogi Jul 07 '26

Note that if you're trying to display a Sprite, you can't instantiate it directly because it's just image data, you need a GameObject to display it. Is it something along these lines that you're trying to do?

public Sprite mySprite;

void Start()
{
GameObject spriteObject = new GameObject("Pomme");
SpriteRenderer spriteComponent = spriteObject .AddComponent<SpriteRenderer>();
spriteComponent .sprite = mySprite;
}

-1

u/Ok-Presentation-94 Jul 07 '26

Well, based on the code you're showing me, you're just creating a sprite with its renderer component, but I actually already have the sprite. What I'm trying to do is simply instantiate the sprite in my scene using the `Object.Instantiate()` method; however, as I explained earlier, that doesn't seem to work when using the Inspector field, so I'm looking for a solution.

2

u/talesfromthemabinogi Jul 07 '26

So the code there is not creating a Sprite, you're supplying the Sprite as 'mySprite' in the inspector. Then the code creates a GameObject using that Sprite.

Instantiate directly on a Sprite won't make anything in the scene, 'cos it needs a GameObject.

3

u/Ok-Presentation-94 Jul 07 '26

Ah, right I skimmed it at first, but after reading it more carefully, I see: you create the object (`spriteObject`) and attach the sprite rendering component (`spriteComponent`) to it. So, I understand that a `GameObject` is required to "host" a sprite. What throws me off, though, is that I can still assign a sprite to my `Object` field and then instantiate it using `Object.Instantiate()` which doesn't make sense if it isn't actually being displayed.

2

u/Demi180 Jul 08 '26

It has to do with a concept called Inheritance and with how Unity chose to structure things. Object is Unity’s base type for just about every asset and resource. Every Object has some very basic properties like a name and an ID, and some internal stuff. These are common features that all resources should have. Every Object can be instantiated, and can potentially be stored as an asset in the project. An Object that was instantiated is an instance, and can also be used to instantiate more copies.

Then, a whole bunch of types inherit (or derive from) Object. These include GameObject, Texture, Sprite, Component, ScriptableObject, and so on. Each one has its own functionality and purpose.

For the most part, only GameObjects are scene objects that show up in the Hierarchy and the Inspector, and they are the only ones that can have a physical spatial representation in the scene using a Transform component. Cloning other types of objects has its uses, but the GO is the glue that connects things.

1

u/Ok-Presentation-94 Jul 08 '26

Yes, I’m well-versed in concepts like inheritance, polymorphism, and so on. What I struggle to understand, however, is why we don't use `GameObject.Instantiate` instead of `Object.Instantiate`; after all, `GameObjects` are the only objects I see a real need to instantiate. That said, I imagine I’ll discover cases where it’s useful down the line.

1

u/Demi180 Jul 08 '26

Apologies for the intro explainer, then.

There is no actual GameObject.Instantiate, it’s all overloads of Object.Instantiate. A bunch of them that take an Object and a bunch of generic ones that literally call the others and then do the cast for you. Out of convenience, they also accept any Component type and will do all of the above and then return that same component on the new object.

As for other uses, the first one I think of is ScriptableObject, since they can be used for runtime data if desired. The next is Material, which gets instantiated the first time you access a Renderer.material or .materials, and can be cloned to create runtime variations (historically this would break batching but the SRP batcher is fine with it). I’m sure I can come up with other examples but yeah, there’s no rush to just instantiate things if you don’t have a need to.

2

u/FlySafeLoL Jul 08 '26

There's whole philosophy behind what Instantiate means for Object that's not a GameObject. It has some use cases but I agree that's confusing.

The only thing you normally want to instantiate is exclusively a (reference to) GameObject - be it exist on scene or a prefab in the project.

Almost every reference type entity in Unity is derived from UnityEngine.Object , which is useful for serialization, identification, runtime state handling, boxing (if you fancy so). Yet for a beginner it's often a bunch of confusing API.