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 /
  • Help Room /
avatar image
0
Question by Kimphoe · Sep 21, 2016 at 11:26 AM · editorprefabeditor-scriptingprefab-instanceeditorscript

Editor script: Variable created on gameObject in editor set to missing on runtime

I have created a script for creating roads in a grid pattern. I have three scripts which I've pasted below.

  1. RoadLogic is on every road game object giving it the 4 game object variables north, east, south, west for storing the road pieces that's connected around it.

  2. ObjectBuilderScript is the script for creating the actual roads. Every road piece have 4 connector game objects as children which has the ObjectBuilderScript on them, the connector game objects are spheres placed in each cardinal direction.

  3. ObjectBuilderEditor is just a editor script for creating a button in the default inspector.

So what's supposed to happen is that I select one of the connector spheres. Choose a prefab and press build object. The prefab should be placed at spawnPoint and be parented to an empty object called "roads". And this works on some level. The problem occurs when going into runtime, it seems like all the roads lose their north, south, west, east variable connections. Instead they display as missing.

Any help is appreciated, thanks!

Update It seems like the game objects remember which game object created them. It seems game objects only lose connection to objects they create. So for example an intersection which created 4 roads going from it lost all it's connections, but the roads created from the intersection remember that they were created from the intersection, but not which roads they created in front of them.

roadLogic:

 using UnityEngine;
 using System.Collections;
 
 public class roadLogic : MonoBehaviour {
 
     public GameObject north, east, south, west;
     
 }

ObjectBuilderScript:

 using UnityEngine;
 using System.Collections;
 using UnityEditor;
 
 public class ObjectBuilderScript : MonoBehaviour {
     public Object m_obj;
     Vector3 m_spawnPoint;
     GameObject m_newNeighbour;
     roadLogic m_parentRoadLogic;
     roadLogic m_neighbourRoadLogic;
     GameObject m_parent;
 
     public void BuildObject()
     {
         switch (gameObject.name)
         {
             case "north":
                 m_spawnPoint = new Vector3(transform.position.x, transform.position.y, transform.position.z + 6);
                 m_newNeighbour = PrefabUtility.InstantiatePrefab(m_obj) as GameObject;
                 m_newNeighbour.transform.position = m_spawnPoint;
                 m_newNeighbour.transform.parent = GameObject.Find("roads").transform;
                 m_neighbourRoadLogic = m_newNeighbour.GetComponent<roadLogic>();
                 m_parentRoadLogic = transform.parent.GetComponent<roadLogic>();
                 m_parentRoadLogic.north = m_newNeighbour;
                 m_neighbourRoadLogic.south = transform.parent.gameObject;
                 break;
             case "east":
                 m_spawnPoint = new Vector3(transform.position.x + 6, transform.position.y, transform.position.z);
                 m_newNeighbour = PrefabUtility.InstantiatePrefab(m_obj) as GameObject;
                 m_newNeighbour.transform.position = m_spawnPoint;
                 m_newNeighbour.transform.parent = GameObject.Find("roads").transform;
                 m_neighbourRoadLogic = m_newNeighbour.GetComponent<roadLogic>();
                 m_parentRoadLogic = transform.parent.GetComponent<roadLogic>();
 
                 m_parentRoadLogic.east = m_newNeighbour;
                 m_neighbourRoadLogic.west = transform.parent.gameObject;
                 break;
             case "south":
                 m_spawnPoint = new Vector3(transform.position.x, transform.position.y, transform.position.z - 6);
                 m_newNeighbour = PrefabUtility.InstantiatePrefab(m_obj) as GameObject;
                 m_newNeighbour.transform.position = m_spawnPoint;
                 m_newNeighbour.transform.parent = GameObject.Find("roads").transform;
                 m_neighbourRoadLogic = m_newNeighbour.GetComponent<roadLogic>();
                 m_parentRoadLogic = transform.parent.GetComponent<roadLogic>();
 
                 m_parentRoadLogic.south = m_newNeighbour;
                 m_neighbourRoadLogic.north = transform.parent.gameObject;
                 break;
             case "west":
                 m_spawnPoint = new Vector3(transform.position.x - 6, transform.position.y, transform.position.z);
                 m_newNeighbour = PrefabUtility.InstantiatePrefab(m_obj) as GameObject;
                 m_newNeighbour.transform.position = m_spawnPoint;
                 m_newNeighbour.transform.parent = GameObject.Find("roads").transform;
                 m_neighbourRoadLogic = m_newNeighbour.GetComponent<roadLogic>();
                 m_parentRoadLogic = transform.parent.GetComponent<roadLogic>();
 
                 m_parentRoadLogic.west = m_newNeighbour;
                 m_neighbourRoadLogic.east = transform.parent.gameObject;
                 break;
             default:
                 break;
         }
         
     }
 }


ObjectBuilderEditor:

 using UnityEngine;
 using System.Collections;
 using UnityEditor;
 
 [CustomEditor(typeof(ObjectBuilderScript))]
 public class ObjectBuilderEditor : Editor
 {
     public override void OnInspectorGUI()
     {
         DrawDefaultInspector();
 
         ObjectBuilderScript myScript = (ObjectBuilderScript)target;
         if (GUILayout.Button("Build Object"))
         {
             myScript.BuildObject();
         }
     }
 }






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 Kimphoe · Sep 21, 2016 at 11:28 AM 0
Share

Sorry for the somewhat messy ObjectBuilderScript, I will put the functionality in a function ins$$anonymous$$d of rewriting it 4 times.

0 Replies

· Add your reply
  • Sort: 

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

92 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

Related Questions

Dragging prefab onto object through code 1 Answer

Storing array of game objects in editor mode and click play corrupt the array.,Can't get game object from array after click on the start. 0 Answers

Why does GameObject.FindWithTag() get a wrong instance? 2 Answers

Editorscript: How to reference prefabs in editor not in awake or start 0 Answers

Manually unselect a handle in the editor from a script 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