Instantiated prefab loses script reference - unity3d

My prefab will not retain the move joystick reference. If I add the reference back while running, the everything works, but the instantiated prefab will lost the reference once I delete it from the screen, or run the project.
I was sure to hit Apply on the prefab, but essentially, its acting as though I didn't.
the reference is added here...
but gone on the instantiated object...
Note that the ship object that I drag into the scene still has the reference, but the instantiated ship does not.
I've tried this with the joystick container as a prefab and not a prefab.

This is expected behavior
Prefabs cannot maintain references to objects in the scene, as when they get instantiated, there's no guaranteed that that object still exists.
You will have to assign the reference to the script when you instantiate the prefab. You can do this by calling instance_obj.GetComponent<Move>().moveJoystick = ...

Related

Unity : Destroy a GameObject that is part of a prefab

The console shows me this error :
Cannot destroy GameObject that is part of a prefab instance. UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)
I'm confused. The Logs don't tell which prefab or GameObject is involved.
Can u help me ?
Thanks a lot for answers !
Many times when you edit prefabs you will notice that some of them can be blue, while others are with no color. The blue means that they maintain the reference to the original prefab in the project and that they are not a copy, so any change made to them wil be done to the original prefab itself, so the original prebab from which the copies (clones) are made from.
To avoid this, you need to unpack the prefabs, so that you manipulate the copy instead of the original reference.
Probably the error is due to the fact that you are destroying some prefab that is not unpacked, so not destroyable by the runtime GameObject.Destroy that is to destroy a cloned copy from the scene.
Looks like you trying to destroy part of the prefab that is not instanced to the scene. If you need to modify some prefab in editor scripts please refer to PrefabUtility but be aware that this class is accessible only from UnityEditor.
Otherwise please double check your code maybe you using the wrong reference to object after prefab instantiation.
Had this issue recently. A nested prefab was missing in one of the prefabs. Happened when an old prefab was deleted. Check your scene for "Missing Prefab"

Instantiate original Prefab based on an already existing Prefab in game

I am using Prefabs for game levels.
To debug a level I just drag the prefab into Hierarchy view and press [play].
To test level I need an option to replay the prefab, but since I drop it into Hierarchy I need a way to grab the original prefab and Instantiate it again. (and destroy current active prefab).
To find current Active Prefab on Play I just use:
GameObject gameObj= GameObject.FindWithTag("Level");
How do I get the original Prefab and Instantiate it again when pressing 'REPLAY' (button to reload prefab).
I did try to use GetCorrespondingObjectFromOriginalSource
https://docs.unity3d.com/ScriptReference/PrefabUtility.GetCorrespondingObjectFromOriginalSource.html
This will always return null.
Objects in the scene don't have references to the prefabs they come from at runtime. Unity breaks these references when the scene is run (or built). PrefabUtility is an editor-only class for writing editor code.
If you want to do something similar, I'd suggest creating another script that has a reference to the prefab you wish to spawn in. This script can then instantiate that prefab in as a child when the game starts (in Awake or Start) and could have a method on it for destroying the instance and instantiating it again when you want to restart your level.

Why isn't Unity allowing me to apply certain changes to a prefab?

I have a prefab called square in the asset tray, I drag it into the scene to create an instance of the square prefab (in the hierarchy it is highlighted blue and has the select, revert and apply buttons in the properties). If I move/resize the instance and then press apply, the changes are applied to the prefab.
However, the square prefab has a script with a public game object. I drag the game object from the hierarchy into the slot in the instance's properties tab and is shows that the script is now referencing the actual gameobject. However, when I press apply this change isn't applied to the prefab (the prefab's script still isn't referencing any actual gameobject). All other changes to the prefab are applied.
I have tried dragging the instance into the asset tray to create a new prefab with the changes, but, as soon as I do, the script no longer references the game object.
I shouldn't have to create an entirely new prefab every time I add a public variable to a script so why can't I apply this change?
Ok you can't really drag an instance to a prefab. As a rule of thumb keep instances with instances and prefabs with prefabs.
I would instead find the instance from the prefab when this one is instantiated, with a method like findobjectoftype, or gameobject.find. Let me know if you want me to expand the answer
Solved: Prefabs can only reference other prefabs since there might not be an instance. Made the actual gameobject into an instance of a prefab and applied the prefab to the script
Prefabs are Assets. You can reference only other Assets to an Asset, never instances of Assets.

Can't apply prefab changes

i have a problem when try to apply changes.
I have a GameObject in the hierarchy, i put other GO in a custom script and push apply button. All has saved, but 2 game object can't apply canges.
Thanks in advance.
If you add new objects to the transform hierarchy, those new objects can't reference the prefab because they don't know they're part of the prefab. You can tell this because in the screen hierarchy list the prefab items are blue and the rest are not. You have to save the changes from an object that is part of the prefab first or redefine the prefab by dragging the parent object onto the prefab on the project list. Because a direct save was possible, it will overwrite.
The same is still essentially true for removing objects from the prefab hierarchy, except Unity knows that this is a breaking change (because it wouldn't be mappable to the prefab anymore) and informs you that doing so well remove ALL of the objects from the prefab upward reference and you can only save the changes by redefining the prefab by dragging it from scene to project (and possibly receive another warning about the objects being different: are you sure you want to overwrite?).
Likely those Text and Transform objects are found outside of the prefab. Prefabs can't reference external objects, since there's no guarantee they'll be available for every instantiation of the prefab.

Unity 5 // when enemy spawns , Enemy target is none?

I've got a basic AI script where I'll assign an object for the enemy to chase.
The problem is, when I turn the enemy into a prefab, the target assignment becomes blank and I cannot alter it while it's a prefab. I've tried to assign the object within the code itself but I'm not completely sure how to do so (I've tried a number of things but nothing has panned out).
Any tips on how to fix the first problem, or just how to assign a target within code would be very helpful. JavaScript would be the preferred language for code.
It's the intended behaviours of prefabs.
You can't link a gameobject which belongs to the scene, to a field of a prefab in your assets since it must be completely independant from any instance of your scene. Prefabs are intended to be instantiated, and then, you will be able to assign (through code) the public field you want to your instantiated enemy. See a prefab as a file in your HDD you can instantiate from.
Without any code it's hard to help, but I guess you can do something similar to this :
var newEnemy : EnemyAI = Instantiate(enemyPrefab);
newEnemy.target = GameObject.FindWithTag ("Player").GetComponent.<Transform>();
I'm not fluent at all with Unity script.
You are still able to "instantiate" the enemies directly in the scene by dragging and dropping the prefab into your scene, and you will be able to assign the Target of your instantiated prefab.