Unity Creating Invisible Clone (C#)

Hey Guys

I have a enemy script that looks at a variable from the player script. So I have local variables in the enemy script for the player, and various scripts attached to the player. The problem is, sometimes Unity will create an invisible clone of the player object that doesn’t show up in the hierarchy, but it is what Unity finds first when looking for a player object. So all of my script references get broken and throws up NullReferenceExceptions because this clone has no scripts attached (i guess). I have noticed that this always happens whenever I mess with Mecanim settings.

Here is the code:

void Start () 
	{
		player = GameObject.FindGameObjectWithTag("Player");  //player is the Player in the scene.
		playerScript = player.GetComponent<TopLevelPlayerScript>();  //playerScript is the TopLevel script
		combatScript = player.GetComponent<BaseCombatSystem>(); //combatScript is the BaseCombatScript.
		healthScript = GetComponent<Health>();
		anim = GetComponent<Animator>(); //This is the animator component for the Orc.

		thisRenderer.material = diffuseMat;

		anim.applyRootMotion = false;  //This is here just for testing purposes.  I didn't want some of the animations to apply root animation.
	}

	void Update () 
	{
		print (player.name); //This is for debgging purposes

		if(combatScript.target == this.gameObject)  //If the player is highlighing us
		{
			thisRenderer.material = outlinedMat;  //Change the material to the outline material
		}
		else  //If we aren't being looked at
		{
			if(thisRenderer.material != diffuseMat) //Switch to diffuse.
			{
				thisRenderer.material = diffuseMat;
			}
		}

I’ve read around online, and discovered the fix is to restart Unity. Restarting unity fixes the problem, but any time I change anything in Mecanim the error pops up again, forcing me to restart unity. Its kinda annoying.

So do you think this is a me problem, or a bug in Unity? Should I write a bug report? Do you need any information to help?

Thanks in advance

I’m not entirely sure if it’s a bug, but your implementation seems fine.

If Unity really is making a duplicate object, the workaround is to get all the objects with the player tag, and search them for the valid player object.

    GameObject[] playerList = GameObject.FindGameObjectsWithTag("Player"); // Get a list of all the objets with the player tag into an array
    for (int i = 0; i < playerList.Length(); i++)
    {
         if (playerList*.GetComponent<TopLevelPlayerScript>() != null) // See if we can find the script on the returned object*

{
player = PlayerList*;*
break;
}
}
if (player == null) { // Check to make sure we found the player.
Debug.LogError(“Unable to find player object!”);
return; // Prevent any NullExceptionErrors
}

I’ve been having the same error too, a temporary fix for me is to just restart Unity, but it doesn’t really fix the issue overall. I tried reinstalling, haven’t had the problem again yet, but would like to find a solution as well.

I have also had this issue, restarting Unity now. Possibly a bug, hopefully will be resolved in the 4.x versions.

Attack of the clones!!

Same issue, same solution, restart unity… I guess when you have to restart the editor to makes your game run fine, it is safe to call it a bug. The only editor scripts i have now, i didn’t had them when this issue started to show up.

The same have happened to me a couple of times lately. Also fixed by restarting the editor.

On thing I think I noticed is that it happens if I change another object to the Player layer, make it prefab, remove the layer to the prefab. Seems like cache problem to me, not sure.

Restarting Unity works, also you may want to change the Tag if you have already saved the scene and now problem is persisting. Something gets broken when you use tag to find an object and object has an animator and when you mess with mechanim.

It is a bug. Because when you change the tag and look for that tag next time same code works perfectly. Though I guess you can avoid it by looking for all objects and then checking if it has (clone) property in name. better yet just change the tag (Unity 5.3 here - the stable built :stuck_out_tongue: )