Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 roamcel · Aug 24, 2011 at 03:49 PM · instantiateprefabnetworkreference

Brain freeze: declaring a prefab inside the same prefab

This might seem like a pretty noobish and common question. And perhaps it is, but I haven't found an approach to the same subject so I hope some of you already faced this dilemma.

I have a simple prefab which, because of a multiplayer necessity, needs be instantiated dynamically.

The point is, this object is not just dynamic in the sense that it can be randomly spawned. The very reference for it in the script does change. So as an example, I have:

 ...
 class explosion
 Transform explosiontype;
 ...

Now my prefabs are in the sort of

nuclear

daisycutter

napalm

but of course since I need to instantiate that prefab at some point (they are assigned into other gameobjects arrays)

I have something like

 nuclear prefab => explosiontype => nuclear prefab <= dragged into the public slot in the inspector
 daisycutter prefab => explosiontype => daisycutter prefab
 napalmprefab => explosiontype => napalm prefab

Well, basically this works. But it feels dirty. I am wondering if any of you faced this dilemma already, and if this is the preferred way to solve this necessity. Again, the 'problem' is the fact that in a multiplayer setting, Network.Instantiate needs a PREFAB reference, whilst the normal instantiate can do with an object.

thanks

Comment
Add comment · Show 6
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 roamcel · Aug 24, 2011 at 05:09 PM 0
Share

As a matter of fact, I now realize it doesn't work:

http://forum.unity3d.com/threads/57312-Reference-to-prefab-changing-to-clone-self-reference

this is so fucking horrible a problem that I don't even know what to look at for a solution, since it's so ridiculously counterintuitive and buggy as a behavior that there was no possible good planning ahead that could have prevented it. I basically would need to change two dozen classes to create a master prefab reference... and even so I would be halfway through a solution.

Hope that some of you unity gurus out there faced this problem and found a viable solution (i.e. breaking prefab connection, perhaps?)

avatar image Waz · Aug 25, 2011 at 11:31 AM 0
Share

It's not clear to me why the explosion needs a reference to itself. Surely whatever Instantiated the original explosion would already have that, and should be also responsible for creating the network instances too?

avatar image roamcel · Aug 25, 2011 at 12:39 PM 0
Share

@Warwick Allison sorry if it wasn't clear, it's because of a stupid idea behind the classing, where an explosion spawns sub explosions, for which I need the base prefab to be referenced. Sadly, I two other main classes upon this logic, and rewiring them to use a global prefab reference array or list would be plain crazy.

avatar image PrimeDerektive · Aug 25, 2011 at 01:35 PM 0
Share

Well if you admit that the idea is stupid and you're too lazy to fix it, it's a little difficult to help, haha. It's UnityAnswers, not UnityHacks.

avatar image roamcel · Aug 25, 2011 at 02:16 PM 0
Share

@Derek Traver lol it is not a stupid idea per se. It's stupid because it stuck me in a problem that should not have existed to start with. And lol @ being lazy. We're talking about thousands of lines of raw code which again would never have needed an unnecessary fix for a buglike behavior. I'm sure that sits well with you too!

Show more comments

1 Reply

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

Answer by Waz · Aug 25, 2011 at 02:36 PM

At the time the original object is created:

 var e = Instantiate(explosion,p,r);
 e.explosionType = explosion;

I would even unset them in the inspector, to ensure you get a crash if this is missed anywhere.

Comment
Add comment · Show 9 · 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 PrimeDerektive · Aug 25, 2011 at 04:07 PM 0
Share

I was going to say something like this.. then have e load the right prefab. Although I don't see why he can't just have references to all the explosions prefabs, do a switch on explosionType and instantiate the right prefab right at the source.

avatar image PrimeDerektive · Aug 25, 2011 at 04:08 PM 0
Share

Also, he mentioned he's using Network.Instantiate, so in this case he'd have to hit the newly Network.Instantiated "e" object with an RPC to set explosionType.

avatar image PrimeDerektive · Aug 25, 2011 at 04:10 PM 1
Share

On second though... operating under the assumption that these explosions being instantiated by a previously Network.Instantiated object (like a missile or whatever), and assu$$anonymous$$g that missile or whatever's rigidbody or transform is being sync'd over the network, there's no reason the explosion needs to be Net Instantiated, each client could just do that locally.

avatar image Waz · Aug 26, 2011 at 06:32 AM 1
Share

Sounds to me like you are way too shy of refactoring. Refactoring should be done as soon as proven necessary, yet it seems you're letting this cause more knock-on problems than the effort of simply fixing it. With languages like C# and UnityScript with #pragma strict, refactoring is mostly a matter of making the core change then doing the monkey-work of getting everything to compile again.

avatar image roamcel · Sep 01, 2011 at 02:18 PM 1
Share

It actually works. I had to redesign just half of my classes but this can save you some trouble. Thanks @Warwick Allison

Show more comments

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

gameObject are not referenced 2 Answers

Reference without Transform 1 Answer

Can't reference network instantiated Game Object 0 Answers

Instantiating a prefab that needs a public reference to another prefab 3 Answers

Network Instantiate PlayerPrefab Problem 2 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