- Home /
Problem with assigning a something to a prefab
I created a question here last night and was led to the real problem behind that question but it still never got answered. And now more of the same problems are popping up in my unity file. I have tried googling everything I could think of and nothing that helped. I will post the link to the other question for reference at the bottom if needed. So here is the problem I am having:
When a "clone" of my player is instantiated the clone doesn't have everything the original "player" has that was in my scene. Example one: When my player object respawns it should have an empty gameObject attached as a child that is tagged target. It doesn't have it and now one of my enemies doesn't "LookAt" with my script but it did before the player died.
Example two: I have an audio source that is attached to the player and a script that is on the player that tells where that audio source is to enable it. It works perfectly until the player respawns after dying, then I get a null reference exception because the part in the inspector where I had assigned it is set to none when I checked.
It's like its not saving the prefab. And yes I first "applied" the "player" in my scene, then since that didn't work I tried dragging and dropping the player prefab from the scene back onto the player prefab in the project folders area. It shows that its there in the project view and when I drag it back into the scene it has everything on it, but again when a clone is instantiated it doesn't have certain things.
Here is the link to the previous question since it has some of the scripts (even though I don't think it is a problem with the scripts): http://answers.unity3d.com/questions/417949/semi-quick-question-about-lookat-script-after-resp.html
I guess I should probably ask why for it to be a question. So why is my prefab not saving when it gets instantiated as a clone? And is there a way around this or to fix it?
Answer by Loius · Mar 16, 2013 at 07:17 PM
If you instantiate from a prefab, you'll always get a copy of the prefab. In order to not get a full copy of the prefab, you'd have to alter the prefab - are you using the prefab instead of the instantiated object somewhere?
Failing that, if you have 'initial spawn' code AND 'respawn' code, then your respawn code is doing something it shouldn't and we'd need to take a look at it.
Also, pause the game just after a respawn and examine the clone - find the exact differences between it and your desired outcome, and see if you can work it out from there.
I have already looked at the clone and the original and I do see the missing items but I still don't see how to fix it. I dont have an initial spawn code (the player is already in the scene when the game starts) but here is my Respawn code (the player is attached to the main camera):
#pragma strict
private var player : GameObject;
var lives : int = 3;
var playerspawnPoint : GameObject;
var playerOne : GameObject;
var respawnStarted : boolean = false;
function Update()
{
player = GameObject.FindWithTag("Player");
if(!player && !respawnStarted)
{
RespawnInstructions();
}
}
function RespawnInstructions()
{
respawnStarted = true;
lives--;
if(lives <= 0)
{
yield WaitForSeconds(3.5);
Application.LoadLevel("Aliens$$anonymous$$ain$$anonymous$$enu");
}
else
{
yield WaitForSeconds(2);
var newPlayer : GameObject = Instantiate(playerOne, playerspawnPoint.transform.position, playerspawnPoint.transform.rotation);
newPlayer.transform.parent = Camera.main.transform;
}
respawnStarted = false;
}
Thank you for your help I guess I am just too tired and have been working on this for too long I am starting to miss the small things. The problem was I had the "plane" under the camera as a prefab and a plane prefab itself and was instantaiting the plane as a replacement ins$$anonymous$$d of the camera "plane". I must be losing it. Sorry everyone that was helping me haha. But thank you anyway.
But thank you for your answer Loius if it wasn't for the pause and look at it again anyway line(which I had been doing for the past two days) I wouldn't have seen that I had another prefab of the plane hidden away. I had pretty much given up on that.
Your answer
![](https://koobas.hobune.stream/wayback/20220613094528im_/https://answers.unity.com/themes/thub/images/avi.jpg)