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 /
  • Help Room /
avatar image
0
Question by Malts_ · Mar 25, 2021 at 09:02 PM · editorarraysserializationcustom editorcustom class

How Can I Stop my Array Being Cleared on Runtime via Serialisation?

So I'm designing a robots simulation in Unity, which at the moment just instantiates a square grid and performs some A* pathfinding on it. The grid is instantiated via a custom editor so that I can then set up robots, obstacles, etc.

Before I press play, I have a NavGrid object which holds an array of its associated nodes (i.e. grid tiles). Note that these are of a custom class called Node rather than GameObjects. However, when runtime starts, the array is cleared and its length becomes zero.

I have tried serialising the array and the Node object but to no avail. My next thought would be to instantiate the Nodes as ScriptableObjects, but as they need to reference the GameObject to which they are attached, it looks like this is unfeasible. I think I'm missing something fundamental to serialisable variables, since I've difficulty believing this is really this awkward.

Any help would be appreciated.

NavGrid code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 
 [System.Serializable]
 public class NavGrid : MonoBehaviour
 {
 
     public int gridSize = 16; // size of the square grid
     [SerializeField] public Node[] nodes;
     public GameObject nodePreset;
     public float nodeSize = 1; //size of square node in actual lentgh units
 
     public void BuildGrid()
     {
 
         print("Hello");
         if (Application.isPlaying)
         {
             Debug.Log("ERROR: Cannot build new grid at runtime.");
             return;
         }
 
         for (int i = nodes.Length-1; i >= 0; i--)
         {
             nodes[i].DestroyNode();
         }
 
         nodes = new Node[gridSize*gridSize];
         
 
         for (int i = 0; i < gridSize; i++)
         {
             for (int j = 0; j < gridSize; j++)
             {
                 Vector3 nodePosition = new Vector3(j * nodeSize + nodeSize / 2, transform.position.y, i * nodeSize + nodeSize / 2);
                 GameObject g = Instantiate(nodePreset, nodePosition, Quaternion.Euler(-90, 0, 0), transform);
                 Node n = g.GetComponent<Node>();
                 n.associatedGrid = this;
                 n.id = j + i * gridSize;
                 n.Rescale(nodeSize);
                 n.SetGoal(false);
                 nodes[n.id] = n;
             }
         }
     }
 }
 

Node code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [System.Serializable]
 public class Node: MonoBehaviour
 {
     public int id;
     public bool isGoal;
     public bool isObstacle;
     public int team;
     public int priority;
     public GameObject robotPrefab;
     public NavGrid associatedGrid;
 
     public Route asscociatedRoute;
     public HolonomicRobot associatedRobot; //this will have to be changed to type 'robot' upon implementation of that class
    
     public void DestroyNode()
     {
         DestroyImmediate(this.gameObject);
     }
 
     public void Rescale(float newSize)
     {
 
         float size = this.GetComponent<Renderer>().bounds.size.x;
 
         Vector3 rescale = transform.localScale;
 
         rescale.x = newSize * rescale.x / size;
         rescale.y = newSize * rescale.y / size;
         rescale.z = newSize * rescale.z / size;
 
         transform.localScale = rescale;
     }
 
     public void ToggleGoal()
     {
         UnityEngine.Debug.Log($"Toggled node #{id} as isGoal.");
         SetGoal(!isGoal);
     }
 
     public void SetGoal(bool trueFalse)
     {
         isGoal = trueFalse;
         SpriteRenderer goalRenderer = gameObject.GetComponentsInChildren<SpriteRenderer>()[1];
         goalRenderer.enabled = trueFalse;
 
         string setRemovedString = (isGoal) ? "Set" : "Removed";
         UnityEngine.Debug.Log($"{setRemovedString} goal at node #{id}.");
     }
 
     public void ToggleRobot()
     {
         bool robotPresent = associatedRobot != null;
         SetRobot(!robotPresent);
         UnityEngine.Debug.Log($"Toggled robot on node #{id}.");
 
 
     }
 
     public void SetRobot(bool trueFalse)
     {
         //if false, check if associated robot exists and destroy if so
         //if true, spawn robot directly on tile and make it associated robot
 
         string setRemovedString = (trueFalse) ? "Created" : "Destroyed";
         UnityEngine.Debug.Log($"{setRemovedString} robot at node #{id}.");
 
         if (!trueFalse) {
             DestroyImmediate(associatedRobot.gameObject);
             associatedRobot = null;
 
         }
         else if(associatedRobot == null)
         {
 
             GameObject g = Instantiate(robotPrefab, transform.position + Vector3.up * 0.1f , Quaternion.identity);
             associatedRobot = g.GetComponent<HolonomicRobot>();
             associatedRobot.currentNode = this;
             associatedRobot.associatedGrid = this.associatedGrid;
 
         } else
         {
             UnityEngine.Debug.Log($"ERROR: Robot already present at node #{id}.");
         }
     }
 
     public void ToggleObstacle() {
 
         SetObstacle(!isObstacle);
 
     }
 
     public void SetObstacle(bool trueFalse)
     {
 
         isObstacle = trueFalse;
         SpriteRenderer s = gameObject.GetComponent<SpriteRenderer>();
         s.color = trueFalse ? new Color(80f/255f, 68f/255f, 68f/255f, 1f): new Color(1f, 1f, 1f, 1f);
         
 
     }
 
 
 }


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

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

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

Related Questions

How to save a two-dimensional array, as part of the variable inspector? 0 Answers

Array of custom classes not saving to Playmode 0 Answers

A script behaviour has a different serialization layout... 7 Answers

HideFlags and Prefabs 1 Answer

Undo Redo Custom Editor Error ("Assertion failed on expression: '!GetUndoManager().IsProcessing()'") 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