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
0
Question by Grim_Reaper · Sep 15, 2014 at 08:20 PM · gameobjectprefabaudiolistener

Is there a difference in adding components to a GameObject?

Hi,

i am working on some kind of "character-management" for my game and i have found out some confusing stuff while creating and instantiating prefabs.

Lets say the player has the possibility to play different characters. Each character will be defined in its own prefab. The character will be shown in the main menu and player will switch to the character when the game starts.

I find it confusing, that there are at least two ways to add components to a Prefab.

First: 1. I create an empty Prefab and drag a model into it. 2. I click on the prefab and click "Add Component" and then select a "Camera"

If i do it this way i do not have any possibility to move around the camera to a position where i want it to be, because if i pull the prefab to the Hierarchy, only the prefab is shown there.

Second: 1. I create an empty Prefab and drag a model into it. 2. I drag the prefab into the scene, go to "Create" in the Hierarchy-panel and select "Camera" 3. I drag the new Camera into my GameObject and save it to the prefab.

Now i can select the Camera and put it on the head for example. I also disable the camera, because i already have a camera in my main menu and i dont want to have errors.

I start the game, instantiate my prefab and get the "There are two audio listeners" - Error anyway (only one camera is enabled).

Somebody can help me and explain why there is this difference and what would be the better approach to create my prefab please? Any ideas why i have the "two audio listeners"-Error?

Thank you, Michael

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by HarshadK · Sep 18, 2014 at 07:12 AM

Ok. First I'll specify the difference between the two methods that you are using:

First: Here you add a Camera component to your player prefab so your camera component is put on your player object itself. In this case the transform of your camera will be the transform of your player prefab and you can not manipulate the transform of your camera independent of your player prefab since your camera will have the transform of your player prefab itself.

Second: Here you are adding a gameobject with a camera component to your player prefab so not your camera is a child of your player prefab. In this case the transform of your camera will be relative to the transform of your player prefab since your camera game object is a child of your player prefab.

If you want to manipulate the transforms for your camera then you should make a camera game object and not add it as a component of your player prefab. This way your camera game object will have its own transform that you can use to move and rotate the camera around. But one drawback of this is that your camera will move relative to your player prefab since your camera is a child of your player prefab.

In some cases, it is also required that you keep your camera a totally independent game object and move it relative to your player via scripting. This allows you to manipulate the transform of your camera relative to world space. Usually this way you can manipulate the Main Camera directly.

As long as how to create a prefab is concerned you can create it as create an empty prefab and add models/components/objects/prefabs to it or you can first set up your game object and convert it to a prefab by dragging and dropping it in project window or you create a prefab by using any other way. This is actually dependent on how comfortable and efficient you are with using a method. These are just different workflows and you choose what suits you the best.

Comment
Add comment · Show 1 · 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
avatar image Grim_Reaper · Sep 18, 2014 at 04:49 PM 0
Share

Hey, thank you for your answeres :)

avatar image
1

Answer by davidc · Sep 16, 2014 at 09:15 AM

Hey Mate, When you add a camera to a gameobject, it comes with an audio listener, you can simply delete the audio listener in whichever camera you like by right clicking on the audio listener and click remove.

How i build prefabs, is i drag them into the Hierarchy, i add whatever i like, then i click "apply" and delete the prefab that in the scene. then when you instantiate it, everything is how you edited it in the Hierarchy. When i add cameras to player prefabs, i put them as children to a gameobject and the player a child to that object as well. so for example.

Player Gameobject (PARENT, has no other components) Camera (CHILD) Player (CHILD, the "actual" player, visuals, scripts, components and everything else for the player goes here)

Thats how i do it. I hope that helped :)

Comment
Add comment · Show 1 · 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
avatar image Grim_Reaper · Sep 18, 2014 at 05:05 AM 0
Share

Hey, thank you for the advice. Have tried it yesterday and it seems to be the best way to expand gameobjects this way. Would like to know the sense of the difference anyway.

avatar image
1

Answer by KazooCar69 · Sep 18, 2014 at 05:45 AM

Hey Michael,

This is a design decision, but given the way that gameobjects that are part of the same hierarchy inherit certain traits like position and rotation, you have to really think about the relationship between them.

For me, I don't like making my camera directly related to the player. What if you want to move the player, but not the camera? With a parent-child relationship, it becomes difficult to do this if you relate them.

Instead, a more effective hierarchy might be three separate prefabs. First, make a prefab for your character and edit it how you want. Then make another prefab for your camera, edit and save that as you want. Now make an empty gameobject called Player in your scene, drag your camera and your character into that, and prefab Player. Now the camera and the character are children of Player, but not directly related to each other. Does this make sense? PS to save you a headache down the road, always set your parent gameobjects to position 0, 0, 0 before messing around with the position of them. A parent's xyz value is world space, but a child's is in local space (relative to the parent.) If you don't know what I mean now, you will once it causes problems, haha.

As for your question about multiple cameras, is your main menu in a different scene? You may want to consider moving it to one. That way you can handle all the logic separately, and you won't have to load your game at the same time as your menu. Application.LoadLevel(index); is what you would use when your player hits Play in the main menu scene, for example. This also solves the multiple camera issue, since you'd have a main menu camera in the menu scene, and a player camera that's instantiated in the next one - gameobjects don't persist through scenes under normal circumstances.

I'm not sure if I helped answer your question.

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

25 People are following this question.

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

Related Questions

Objects flickering between dark and light 7 Answers

Get the name of an instance's prefab at runtime? 0 Answers

Create with code 2.2 pizza not shooting 1 Answer

difference between prefab and gameobject in listening event 1 Answer

Troubles instatiating a gameobject with rotation 0 Answers


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