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 JohnBartle · Mar 19, 2019 at 09:50 PM · instantiate prefabclass object

How to reference a prefab in a class array from a json

I am populating a JSON with an array of classes. Each class contains another array of classes that store variables about an encounter-able event. One of the variables in the class is a public gameobject which contains a prefab with a 3d model component. If I reference the prefab in the JSON is stores it as a volatile instanceID that changes every time the editor is re-launched. The system will work perfectly until the instanceID changes. I'm trying to set it up such that when I add a new event in the future, I don't have to change things in two places. I was thinking of making an independent public gameobject array in an eventmanager script. However that will mean making a new event will require increasing the class array, entering the data. Then adding the prefab to a separate game object array which doesn't sound like much but when there are hundreds of events it will become tedious. Here are the classes:

 [System.Serializable]
 public class ExternalEvents
 {
     public ExternalEvent[] m_ExternalEvents;
 }
 
 [System.Serializable]
 
 public class ExternalEvent
 {
     public GameObject m_EventObject;
     public string m_EventName;
     public string m_Description;
     public int m_ExternalEventCode;
     public int m_MinProbabilityRange = 0;
     public int m_MaxProbabilityRange = 0;
     public bool m_IsUnique;
 }

Here is the method that instantiates the prefab when an event is encountered.

 void CreateRandomExternalEvent()
     {
         int i = Random.Range(0, 1000);
         m_ExternalEventOffset = new Vector3(Random.Range(25, 40), 0, 0);
 
         for (int j = 0; j < m_ExternalEvents.Length; j++)
         {
             if(i >= m_ExternalEvents[j].m_MinProbabilityRange && i <= m_ExternalEvents[j].m_MaxProbabilityRange)
             {
                 Instantiate(m_ExternalEvents[j].m_EventObject, m_MainShip.transform.position + m_ExternalEventOffset, Quaternion.identity, m_LevelObjects.transform);
                 m_EventsActive = 1;
                 break;
             }
         }
     }

So ultimately the questions is, what is an easy and elegant way I can reference the prefab that won't break every time the editor is re-launched. I understand this isn't so much a cut and dry technical question it's more of a request for a suggestion but I don't know anyone who knows a lot about unity so I thought I would try the official forums.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by mchts · Mar 23, 2019 at 09:57 AM

Move your prefabs in a Resources folder under Assets and name them uniquely. Instead of using public GameObject m_EventObject; store the name of the prefab like public string m_EventObject;. And when it comes to instantiate do like following:

 Instantiate(Resources.Load(m_ExternalEvents[j].m_EventObject, typeof(GameObject)), m_MainShip.transform.position + m_ExternalEventOffset, Quaternion.identity, m_LevelObjects.transform);

This line above will look for any Resources folder under Assets. If your prefabs are in a specific path you must add that as a prefix to your m_ExternalEvents[j].m_EventObject. For e.g. "MyAllPrefabs/ExternalEvents/"+m_ExternalEvents[j].m_EventObject. Hope this helps

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

100 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 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 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 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

Problem instantiating a prefab, and storing a reference to it. 1 Answer

How to instantiate an object on multiple positions? 0 Answers

Intantiated objects being created but being invisible...[PICTURES] 1 Answer

why is it slow to instantiate 0 Answers

instantiating prefab does not rotate child objects correctly? 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