Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by hotemup · Jan 05, 2012 at 07:48 PM · gameobjectinstantiatebugcomponent

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");
      }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DaveA · Jan 05, 2012 at 08:42 PM 0
Share

Can you post more code? Like where playerPrefabs, user, and Avatar$$anonymous$$odel are declared?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by swisscoder · Jan 10, 2012 at 06:12 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

AddComponent() causes a "trying to create a MonoBehaviour using the 'new' keyword" warning 2 Answers

Instantiate a GameObject multiple times 2 Answers

How to set/get/change variables for newly spawned gameObjects? 1 Answer

Unable to call another gameObject's script 4 Answers

How to copy a Script as a child of another object 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges