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 ArtOfWarfare · Dec 15, 2015 at 02:06 PM · editor-scriptingnullreferenceexceptionstart

Set Variable on Editor Save - Load in Game?

I would like to bake pathing data into terrain in my game automatically. Whenever the terrain is saved in the editor, I would like to automatically calculate new pathing data and assign it to the terrain so that it can be accessed when my game starts.

To do this, I wrote a class called Pathing which is a MonoBehavior so can be assigned as a component to a GameObject:

 public class Pathing : MonoBehaviour {
     public uint[,] height;

     void Start () {
         print("I have a height array: " + height[0, 0].ToString());
     }
 }

Then I have another called class called PathingGenerator which inherits from AssetModificationProcessor so that it will see when files are saved, which will be the queue to update the data in Pathing.

 using UnityEditor;
 using UnityEngine;
 using System.Collections;
 
 public class PathingGenerator : UnityEditor.AssetModificationProcessor {
     public static string[] OnWillSaveAssets(string[] paths) {

         // Find the terrain
         Terrain[] terrains = Object.FindObjectsOfType<Terrain>();
         if (terrains.Length != 1) {
             Debug.LogError("There should be exactly 1 terrain - " + terrains.Length.ToString() + " found.");
             return paths;
         }

         // Ensure this component exists.
         Pathing[] pathings = terrains[0].gameObject.GetComponents<Pathing>();
         Pathing pathing;
         if (pathings.Length == 0) {
             pathing = terrains[0].gameObject.AddComponent<Pathing>();
             Debug.Log("Just added.");
         } else {
             pathing = pathings[0];
             Debug.Log("Already existed.");
         }

         // Set the values - for now, to test this works, just use fixed numbers.
         // In the future I'll have actual complex equations that run here.
         pathing.height = new uint[2, 2];
         pathing.height[0, 0] = 0;
         pathing.height[0, 1] = 1;
         pathing.height[1, 0] = 2;
         pathing.height[1, 1] = 3;
         Debug.Log("They are set.");
         return paths;
     }
 }

After I save and before I run, this is printed out in the log:

 Already existed.
 They are set.

When I run, I get this message:

 NullReferenceException: Object reference not set to an instance of an object
 Pathing.Start () (at Assets/Scripts/Pathing.cs:10)

What am I doing wrong? Is there a better way to do what I want? (I don't want to run my equations within the game itself. They involve a lot of floating point math which ultimately gets rounded to ints. This potentially will cause massive issues if different platforms have slightly different numbers, which will result in pathing maps that are completely different on each platform. I want to guarantee that they're identical everywhere, thus I'd like to bake the integers into the terrain from someplace within the Editor.)

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 ArtOfWarfare · Dec 16, 2015 at 03:45 AM 0
Share

I believe the problem may be that uint[,] is not serializable. This brings me now to the question: what is and isn't serializable, and what is my best course of action now? It appears I can use a uint[]... coupling that with a uint representing length and/or width may make this easy...

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

31 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

Related Questions

Editor script executing before variable updated 1 Answer

Editor-Script: NullReference after simple script execution 3 Answers

How do I resize the Lightmap Array via Editor Script? 1 Answer

Start/Stop Playmode from editor script 10 Answers

Initialising List array for use in a custom Editor 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