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 Ignacio 1 · Apr 05, 2011 at 08:12 PM · networkinginstantiateprefabreference

Instantiating a prefab that needs a public reference to another prefab

Hi! I'm making a space simulator and I've been tinkering with the networking API in order to add network support to it. Up to this point, I've managed to create a server and make a client connect to it, but I'm having some problems when trying to instantiate prefabs by using Network.Instantiate().

Here's the scenario: I have a "Cockpit" prefab that uses a public variable in order to store a reference to an "AstronautCamera" (which is another prefab that needs to be instantiated dinamically in order to share it with the rest of the network). While I was working in the single-player version (where the elements of the scene were statically placed) I would create the cockpit and the camera using the editor and drag and drop the camera to the public variable slot. Now, I cannot do that when the prefabs are dinamically instantiated, so how can I make the cockpit object hold a reference to the camera after both elements have been instantiated?

A possible solution would be to get a reference to the cockpit's script and manually assign the public variable to the camera's prefab, but I'm sure there's have to be a cleaner way. Do you have any suggestions?

Thanks in advance for your help!

--Ignacio

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
0
Best Answer

Answer by Ignacio 1 · Apr 07, 2011 at 07:14 PM

Thank you all for your answers! I've finally solved it by adding a public function to the Cockpit's script called SetAstronautCamera and I invoke that function right after the network instantiation, like this:

Network.Instantiate(AstronautCamera, AstronautCamera.transform.position, AstronautCamera.transform.rotation, 0);
Network.Instantiate(MRCockpit, MRCockpit.transform.position, MRCockpit.transform.rotation, 0);                      
GameObject MRCockpitInstance = GameObject.FindWithTag("MRCockpit");
GameObject AstronautCameraInstance = GameObject.FindWithTag("AstronautCamera");
MRCockpit MRCockpitScript = MRCockpitInstance.transform.GetComponent<MRCockpit>();
MRCockpitScript.SetAstronautCamera(AstronautCameraInstance);

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
avatar image
1

Answer by AKAssassin · Apr 06, 2011 at 10:18 PM

I have done this quite a bit with the project I am working on.

This works even if you do not currently have any versions of the prefab in the scene.

This is a .js example but you will get the point anyways.

public var cockpit : GameObject;

function Awake(){

 cockpit = Resources.Load("CockpitPrefabNameHere");

}

This will only work if you have your prefabs stored in the Resources folder in your project but putting all your prefabs there will make loading them whenever you need them easier. This way when you Network.Instantiate(), on everyone's game the prefab will load the reference to the other prefab automatically.

Side Note: Network.Instantiate isn't the best way to go about creating things in the game, manually using RPCs to instantiate the prefabs on all the clients and allocating networkviewid's yourself is a much better way of going about it.

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 husbandofemily · Apr 29, 2012 at 06:31 AM 0
Share

This worked for me, to make a prefab instantiate another prefab. Cheers! :)

avatar image
0

Answer by AngryOldMan · Apr 05, 2011 at 10:49 PM

while trying to do things such as this I use this method

var cockpit = GameObject;

 function Update()
 {
     if (cockpit == null)
     {
         cockpit = GameObject.FindWithTag("cockpit");
     }
 }

I've heard that this isn't a very processor efficient way but it's working fine for me and I have used this in nearly every script as nearly everything for me needs to be dynamically assigned. I'm using it in update just incase a player is destroyed and respawned. If you don't have this problem you could use it in function Awake or function Start

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

Solution to MissingReferenceException w/ prefab? 1 Answer

(Clone) from Instantiated Prefabs over Network 1 Answer

The object Player(Clone) must be a prefab in the project view. 1 Answer

Referencing in a prefab 1 Answer

Reference Player When Instantiating A Prefab 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