How can I reference object that is inside of an another prefab?

I’m trying to do put objects inside of the prefab.
I know how “Type Mismatches” work, but I don’t know how to fix this specific one.

I’m referencing a script, that is inside of another prefab, I’m trying to drag this specific object into the script, but it just doesn’t work.

What should I do?

It’s not possible to reference a child object of a prefab from another prefab like this. There usually are better ways to handle this.

What are those ways?

It depends what you want to do exactly. Why are you trying to reference it in the first place?

A prefab can’t hold a reference to a gameObject that’s within a scene. It wouldn’t make sense. What happens if the scene isn’t loaded?. Instead make a fire prefab and then you’ll be able to drag it onto the slug’s ‘On Fire’ slot. Prefabs can reference other prefabs.

Because the prefab’s script is checking for a bool inside of the child’s object.

The script cannot work properly without it.

The gameObject isn’t in scene.

It’s child object of a prefab.

Prefab values don’t change at runtime though, therefore you don’t really need to check for the bool value directly. You should already know whether it’s true or not. Still not sure what your goal is exactly that makes you want to access this script. You likely don’t need to.

A prefab wanting to know the values of a component nested inside another component doesn’t make sense, not in this context. The prefab on disk is a separate instance to any live instance in a scene, so I doubt this will work as you expect it to.

I know prefab values don’t change on runtinme. Spawned objects do.
And I need to check on if bool if true or false if specific action is executed.

I have no idea how to do it without reference.

Spawned objects are totally different objects than the actual prefab. If it was possible to reference the child object of the prefab like you want to, that object’s values will never change, even if the values of any objects spawned from the prefab change at runtime. Do you want to reference a spawned object? And if so, which one? You’ll have to create logic for that. Referencing the script in the actual prefab is incorrect and won’t do what you want.

I don’t mean referencing script in prefab. I mean referencing script in object spawned from prefab.

You will need to do that via code. The component on spawning prefab can reference this component, and then pass a reference to the instantiated prefab.

Yes, and I mean what you’re trying to do would only be referencing the script in the prefab.

For the functionality you really want you’ll have to do that via code when instantiating the prefab instance. Ideally a script in the prefab root object would have a reference to this child object script that you can just access. Otherwise, you can use GetComponentInChildren on the spawned prefab object.

I’m really not sure how Getting component would help me with referencing an object.
Could you give me some example?

For example, let’s say your custom script is called MyComponent:

GameObject newPrefabClone = Instantiate(prefabObject);
MyComponent component = newPrefabClone.GetCoponentInChildren<MyComponent>();

You can then do whatever you want with the component variable.

Thanks!

I’ll try it out.