Weird Glitch When Instantiating a Game Object

I used GameObject.Instantiate to spawn a prefab that was dragged into an array in the inspector. If I had any code that referenced this GameObject in certain ways the code would break - like it had crashed, but no errors were thrown. The ways that I found that crashed it included modifying the name (.transform.name) and Debug.Logging the object. However modifying the position and rotation was OK.

I got the code to work but don’t understand why.

Original Non-Working Code:

GameObject characterObject = GameObject.Instantiate(playerPrefabs[user.AvatarModel], spawnPoints[0].transform.position, spawnPoints[0].transform.rotation) as GameObject;

New Working Code:

Component characterObjectComponent = GameObject.Instantiate(playerPrefabs[user.AvatarModel], spawnPoints[0].transform.position, spawnPoints[0].transform.rotation) as Component;
GameObject characterObject = characterObjectComponent.gameObject;

EDIT:

public class PlayerSpawnController : MonoBehaviour
{
	public Transform cam;
    public Transform[] playerPrefabs;
    public Transform[] spawnPoints;
    public Transform movementPivot;
    public AudioClip spawnSound;
    public float spawnSoundVolume = 0.5f;
    public int spawnRadius = 10;
    public Transform playerCharacter;
    private static System.Random random = new System.Random();
    public Transform playerShadow;
    public Transform remotePlayerMapIcon;
    public Transform localPlayerMapIcon;
    public float mapIconHeight = 500.0f;
    private JibeActivityLog jibeLog;
    public NetworkController netController;
    public GameObject spawnParticles;
    public bool usePhysicalCharacter = false;
    public float remotePlayerNametagFullyVisibleDistance = 10.0f;
    public float remotePlayerNametagFadeDistance = 30.0f;
    public bool enableIdleAnimationWakeup = false;
    public bool enableFlyingAvatars = true;
    public bool showOwnName = true;

    public bool useJibeAvatarControlScripts = true;
    public string[] componentsToDisableForRemotePlayers;

    /// <summary>
    /// Create an avatar for the local player based on the user preferences gathered from the loader scene and (if used) dressing room
    /// </summary>
    /// <param name="user">An IJibePlayer representing the user</param>
    /// <returns>A GameObject representing the player and all associated scripts, effects, and local camera</returns>
    public GameObject SpawnLocalPlayer(IJibePlayer user)
    {
        string localPlayerName = "localPlayer";

        GameObject localPlayer = GameObject.Find(localPlayerName);
		print("before");
		Debug.Log("localPlayer: " + localPlayer);

        //Check if the user has already spawned an avatar - if they have not yet spawned an avatar, this is where the avatar is set up.
        if (localPlayer == null)
        {
			Component characterObjectComponent = GameObject.Instantiate(playerPrefabs[user.AvatarModel], spawnPoints[0].transform.position, spawnPoints[0].transform.rotation) as Component;
			GameObject characterObject = characterObjectComponent.gameObject;
			print("begina");
			characterObject.name = localPlayerName;
			print("beginb");
			Debug.Log("Change name from: " + characterObject.name);
			characterObject.name = localPlayerName;
//			Debug.Log("Change name to: " + characterObject.name);
			characterObject.transform.localScale*=.75f;
         }
        return GameObject.FindGameObjectWithTag("Player");
     }

You use an array of Transforms to store your prefabs:

public Transform[] playerPrefabs;

This does influence your code in 2 ways.

  1. You can only store GameObject with the “Transform” Component in there. (Actually every GameObject I can think of at the moment has a “Transform” Component on it.)
  2. if you take something out of this array, it will be the “Transform” Component, which then has a reference to the GameObject it belongs to.

So to make your code more ideal, you would change your array to hold GameObjects (or even simpler Objects)!

public Object[] playerPrefabs;

Then your original line to get the GameObject from the array should work.