x


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");
     }
more ▼

asked Jan 05 '12 at 07:48 PM

hotemup gravatar image

hotemup
61 2 2 3

Can you post more code? Like where playerPrefabs, user, and AvatarModel are declared?

Jan 05 '12 at 08:42 PM DaveA
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Jan 10 '12 at 06:12 PM

swisscoder gravatar image

swisscoder
380 13 15 23

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5088
x2090
x1680
x510
x302

asked: Jan 05 '12 at 07:48 PM

Seen: 632 times

Last Updated: Jan 10 '12 at 06:18 PM