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 Dravel · Jan 30, 2011 at 04:42 AM · instantiateprefab

Attribute inheritance problem when using Instantiate

Ive got a prefab (a space ship) occasionally Instantiating another of the same kind of prefab (another space ship) from within the instantiator's script. When I do this the new ship that's spawned does not inherit all of the attributes that I assigned in the editor for prefabs of this type, but the attributes of the ship that spawned it. Is there a simple way to tell it to not adopt all of the attributes of its instantiator or must I reasign the variables one-by-one after I've instantiated it.

Also, the variable that's being pointed to Instantiate to tell it what prefab to instantiate was set in the editor and is a reference to the prefab. I'm not (at least knowingly) pointing Instantiate to the transform of the object that's doing the instantiating.

Thanks for any help you can provide,

Dave

Comment
Add comment · Show 3
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 Jessy · Jan 30, 2011 at 07:46 AM 0
Share

$$anonymous$$y first thoughts were squashed by your second paragraph. So I suggest posting some code.

avatar image Dravel · Jan 30, 2011 at 10:16 AM 0
Share

yeah i think that ultimately there must be a problem with instantiating this prefab directly from another instance of the same prefab. Short of anybody knowing exactly why this is a problem ive just started instantiating the prefab from a different class

avatar image Bunny83 · Jan 30, 2011 at 12:36 PM 0
Share

You've got it. Instantiate from another class or use a manager class to store the prefab reference like i showed in my answer.

1 Reply

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

Answer by Bunny83 · Jan 30, 2011 at 12:34 PM

First of all, it's not an attribute. At least in C# an attribute is a special class that can be attached to another class. See Attribute. You talk about member variables of your class.

Anyway, this behaviour is a bit hard to understand. I don't will go into detail now. Fact is if you have a prefab with a script that holds a reference to itself, this reference will be replaced with your object instance at creation time. Just avoid self-pointing references. Use eg. a singleton manager for the reference.

More details on this issue in the Unity Forum

Here a singleton manager example in JS (i don't use JS but i've writting this already for somebody else ;) )

// The simplest singleton without checking // scriptfile is named: "PrefabManager"

static var manager : PrefabManager = null;

var spaceShipPrefab : GameObject;

function Awake() { manager = this; }

Place this script on an empty GameObject and assign your prefabs to your public variables.
In any other script (even in a script on your spaceship) you can do:

Instantiate(PrefabManager.manager.spaceShipPrefab);

to create a new instance.

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

No one has followed this question yet.

Related Questions

Don't render prefab until onCollision - related, parent prefab.. 0 Answers

Why is transform.position of a prefab that's been instantiated different than a gameobject created on the scene, when they are in the exact same position? 1 Answer

Instantiating prefab with certain coordinates problem (c#) 0 Answers

Instantiate prefabs just before it comes into view 2 Answers

Access a function of an instantiated prefab 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