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 CiberX15 · Feb 08, 2014 at 07:38 PM · saveinstancenon-monobehavior

list.Add not accepting new class as instance?

Ok so I am trying to create a save load feature. Still. I am trying to save a list of classes that define the name, position, and rotation of the components. But for some reason when I run it in unity it throws up flags claiming that I am not using an instance of an object.

the function in question goes as follows:

 function AddNewPartData(newName : String, pos : Vector3, rot : Quaternion)
     {
         var NewPartData = new PartData();
         NewPartData.PartName = newName;
         NewPartData.PartPos = pos;
         NewPartData.PartRot = rot;
         ShipData.Add(NewPartData);
     }

As you can see I declare the NewPartData as a new PartData object then try to put it in the list. That's how you instance a non-monobehavior class right? Why is this not working?

Thanks in advance for any help provided

Full Code:

 import System.Collections.Generic;
 import System.Xml;
 import System.IO;
  
 @XmlRoot("ShipSave")
 public class ShipSave
 {
     @XmlArray("ShipData")
       @XmlArrayItem("Part")
     public var ShipData : List.<PartData>;
     
     function AddNewPartData(newName : String, pos : Vector3, rot : Quaternion)
     {
         var NewPartData = new PartData();
         NewPartData.PartName = newName;
         NewPartData.PartPos = pos;
         NewPartData.PartRot = rot;
         ShipData.Add(NewPartData);
     }
     
     function AddNewPartData(newName : String, pos : Vector3, rot : Quaternion, group : WeaponGroup)
     {
         var NewPartData = new PartData();
         NewPartData.PartName = newName;
         NewPartData.PartPos = pos;
         NewPartData.PartRot = rot;
         NewPartData.Group = group;
         ShipData.Add(NewPartData);
     }
     
     public function Save(path : String)
      {
          //TODO if file already exists, delete it first then write new one
          var serializer : XmlSerializer = new XmlSerializer(ShipSave);
          var stream : Stream = new FileStream(path, FileMode.Create);
          serializer.Serialize(stream, this);
          stream.Close();
      }
  
      public static function Load(path : String):ShipSave 
      {
          var serializer : XmlSerializer = new XmlSerializer(ShipSave);
          var stream : Stream = new FileStream(path, FileMode.Open);
          var result : ShipSave = serializer.Deserialize(stream) as ShipSave;
          stream.Close();
          return result;
      }
  
          //Loads the xml directly from the given string. Useful in combination with www.text.
          public static function LoadFromText(text : String):ShipSave
          {
          var serializer : XmlSerializer = new XmlSerializer(ShipSave);
          return serializer.Deserialize(new StringReader(text)) as ShipSave;
      }
 }
 
 public class PartData
 {
     var PartName : String;
     var PartPos : Vector3;
     var PartRot : Quaternion;
     var Group : WeaponGroup;
 }
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
0
Best Answer

Answer by CiberX15 · Feb 08, 2014 at 07:44 PM

I am an idiot.

Literally within seconds of posting this I realized what my problem was.

I forgot to instance the list. Instead of:

  public var ShipData : List.<PartData>;

I should have had this:

 public var ShipData : List.<PartData> = new List.<PartData>();

With that fixed everything seems to be working, yay!

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 gamenovice · Feb 08, 2014 at 07:53 PM 0
Share

i love how you started the answer off XD

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

19 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

Related Questions

[solved] How to save instance of the texture? 2 Answers

Set Dirty on class instance? 0 Answers

PlayerPrefs HighScore problems, it doesn't work. 4 Answers

PlayerPrefs.GetInt won't load when awake. 0 Answers

Getting any type in Unity Inspector 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