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 ExplosionPills · Dec 10, 2013 at 05:39 PM · prefabcomponent

Not understanding GetComponent with prefabs

I copied a project and it creates players using:

 player = ((GameObject)Instantiate(UserPlayerPrefab, vector, quat))
    .GetComponent<UserPlayer>();

UserPlayer is a script. This works, but I don't understand the association between the script (filename is UserPlayer) and the actual game object. The project has prefab/UserPlayer which is a prefab that has a capsule mesh. I wanted to use a more complex character design, so I imported an fbx file, created a prefab from it, and added the UserPlayer script as a component. I renamed the original UserPlayerx and the new one to UserPlayer.

However, when I run the game, when the player is created, it's still using the UserPlayerx prefab (i.e. drawing a capsule). There doesn't seem to be any association between the UserPlayer script and the prefab except that the script is a component of the prefab, but the script is also a component of my new user player, so I'm at a complete loss.

How can I create a component using the new prefab?

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 flaviusxvii · Dec 10, 2013 at 06:20 PM 0
Share

In Unity, when you rename a prefab, the editor will help you out by fixing the references to that prefab in your other scene game objects. I suspect that happened here. Go to whereever you set UserPlayerPrefab in the editor and choose the new prefab.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by hd27 · Dec 10, 2013 at 06:17 PM

Try removing the UserPlayer script from the prefabs and adding it to the Instantiated gameobject; What I mean is:

 GameObject player = (GameObject)Instantiate(UserPlayerPrefab, vector, quat);
 
 UserPlayer userPlayer = player.AddComponent<UserPlayer>();

This will create a new gameobject in the scene from the prefab and then it will add the UserPlayer component to it and will store the component in a variable so you can access it;

I hope this is what you wanted to do.

Comment
Add comment · Show 2 · 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 ExplosionPills · Dec 10, 2013 at 06:30 PM 0
Share

Unfortunately this has the same effect. It still makes a clone of UserPlayerx ins$$anonymous$$d of UserPlayer.

avatar image ExplosionPills · Dec 10, 2013 at 06:37 PM 0
Share

I figured it out. The UserPlayerPrefab was set in the inspector. I had to update it to use the new prefab.

avatar image
0

Answer by guitarxe · Dec 10, 2013 at 07:04 PM

From a similar question I asked here, it looks like you cannot link anything external to the prefab. Thus you could try adding your scripts to an instantiated object from your prefab after you have instantiated it.

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

20 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Acces a component on an instantiated object 1 Answer

cant add script 1 Answer

NullReferenceException from a script after linking prefab with drag and drop in inspector. WTF? 1 Answer

Controlling Animator through Script. I'm stuck please help! 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