• 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
Question by 3dDude · Dec 09, 2010 at 02:08 PM · arrayserializationprivate

Saving array data?

Update

I am updating the particle effect.. in the last part here:

pars[i].minSize = minsize[i] * scale; pars[i].maxSize = maxsize[i] * scale;

     pars[i].worldVelocity = worldvelocity[i] * scale;
     pars[i].localVelocity = localvelocity[i] * scale;
     pars[i].rndVelocity = rndvelocity[i] * scale;

     pars[i].transform.localScale = scaleBackUp[i] * scale;

}

that should be updating it.. sorry if that is not what you meant.

could you not just open unity and try out the script?

sorry if I'm being ignorant but I have only been using unity for about half a year.

Hello,

I am making a script that scales particleFX similar to transform scale so if you change scale to 2 then its twice as big....

I its working prefect except one problem.

Once I enter play mode or delete the particle prefab from the scene and then drag a new one into the hierarchy the data for the scale is lost.

along with this code I have a editor script that calls the UpdateScale function when a button is pressed.

var pars : ParticleEmitter[]; var scale : float; @SerializeField private var minsize : Array = []; @SerializeField private var maxsize : Array = []; @SerializeField private var worldvelocity : Array = []; @SerializeField private var localvelocity : Array = []; @SerializeField private var rndvelocity : Array = []; @SerializeField

private var scaleBackUp : Array = [];

function UpdateScale () {
for(i=0;i<pars.length;i++){ minsize.Add(pars[i].minSize); maxsize.Add(pars[i].maxSize);

     worldvelocity.Add(pars[i].worldVelocity);
     localvelocity.Add(pars[i].localVelocity);
     rndvelocity.Add(pars[i].rndVelocity);
     scaleBackUp.Add(pars[i].transform.localScale);
     pars[i].minSize = minsize[i] * scale;
     pars[i].maxSize = maxsize[i] * scale;

     pars[i].worldVelocity = worldvelocity[i] * scale;
     pars[i].localVelocity = localvelocity[i] * scale;
     pars[i].rndVelocity = rndvelocity[i] * scale;

     pars[i].transform.localScale = scaleBackUp[i] * scale;
 }

}

Editor script:

using UnityEngine;

using UnityEditor; [CustomEditor(typeof(Scale))] public class ScaleEditor : Editor { public override void OnInspectorGUI() { Scale scaleBeingInspected = target as Scale; base.OnInspectorGUI(); if (GUILayout.Button("Update Scale")) { scaleBeingInspected.UpdateScale(); } }

}

So I was wondering if there is anyway to make that scale data get saved....

Hopefully I worded this correctly. I am in a hurry to get this code ready.. so any help would be greatly appreciated!

Comment
Statement

People who like this

0 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Statement · Dec 09, 2010 at 05:43 PM

  • You need to serialize the private variables.
  • You need to use built-in arrays or Lists.
  • Private variables are not serialized (stored) by default.

Update


See this related question I just asked.

You need to explicitly tell unity the type of your array. You have to explicitly type the array with your target type. Simple. Use a built-in array or a List.

For example:

@SerializeField
private var minsize : float[];

Then you can allocate enough memory (since you can't use Add() any longer):

minsize = new float[pars.length];

Another update


You were allocating arrays in the loop. Also I cleaned up some of the code to make it a little more readable. Are you sure that you are updating the emitters somewhere along the way after you've edited them?

var pars : ParticleEmitter[]; var scale : float;

@SerializeField private var minsize : float[];

@SerializeField private var maxsize : float[];

@SerializeField private var worldvelocity : Vector3[];

@SerializeField private var localvelocity : Vector3[];

@SerializeField private var rndvelocity : Vector3[];

@SerializeField private var scaleBackUp : Vector3[];

function UpdateScale () {

 var length = pars.length;

 minsize = new float[length];
 maxsize = new float[length];
 worldvelocity = new Vector3[length];
 localvelocity = new Vector3[length];
 rndvelocity = new Vector3[length];
 scaleBackUp = new Vector3[length];

 for ( i = 0 ; i &lt; length ; i++ ) { 
     var emitter = pars[i];

     minsize[i] = emitter.minSize;
     maxsize[i] = emitter.maxSize;
     worldvelocity[i] = emitter.worldVelocity;
     localvelocity[i] = emitter.localVelocity;
     rndvelocity[i] = emitter.rndVelocity;
     scaleBackUp[i] = emitter.transform.localScale;

     emitter.minSize = minsize[i] * scale;
     emitter.maxSize = maxsize[i] * scale;
     emitter.worldVelocity = worldvelocity[i] * scale;
     emitter.localVelocity = localvelocity[i] * scale;
     emitter.rndVelocity = rndvelocity[i] * scale;
     emitter.transform.localScale = scaleBackUp[i] * scale;
 }

}

Comment
Herman-Tulleken

People who like this

1 Show 6 · 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 3dDude · Dec 09, 2010 at 06:30 PM 0
Share

hello statement.

I tried what you said and no luck :(

I edited my question with the new code maybe you can look at it and tell me if i edited it right?

avatar image 3dDude · Dec 09, 2010 at 06:31 PM 0
Share

also I am using unity basic if that matters...

avatar image Statement · Dec 09, 2010 at 09:24 PM 0
Share

Looking at the documentation again it says "Arrays of a serializable type". I don't know if Array host a "serializable type". Can you change it so that the arrays are typed? Like if your minsize is storing floats can you do private var minsize : float[];

avatar image 3dDude · Dec 09, 2010 at 10:29 PM 0
Share

hmmmm I have tried that but then you cant accesses the Array.Add(); function :( and i need that function for the script to work... any ideas? thanks

avatar image Statement · Dec 09, 2010 at 10:33 PM 0
Share

See my update. You need to allocate the array with the 'new' keyword before using it.

Show more comments
avatar image

Answer by 3dDude · Dec 09, 2010 at 11:46 PM

Update:

ok I am using the exact same code you posted and here is a pic of the Hierarchy:

alt text

ok sorry for answering instead of commenting but that would be the last comment, so then you could not write back.

so still not working.. I might gave overlooked something..

thanks a lot for helping me this!

here is the updated code:

var pars : ParticleEmitter[]; var scale : float;

@SerializeField private var minsize : float[];

@SerializeField private var maxsize : float[];

@SerializeField private var worldvelocity : Vector3[];

@SerializeField private var localvelocity : Vector3[];

@SerializeField private var rndvelocity : Vector3[];

@SerializeField private var scaleBackUp : Vector3[];

function UpdateScale () {
for(i=0;i<pars.length;i++){

     minsize = new float[pars.length];
     minsize[i] = pars[i].minSize;

     maxsize = new float[pars.length];
     maxsize[i] = pars[i].maxSize;

     worldvelocity = new Vector3[pars.length];
     worldvelocity[i] = pars[i].worldVelocity;

     localvelocity = new Vector3[pars.length];
     localvelocity[i] = pars[i].localVelocity;

     rndvelocity = new Vector3[pars.length];
     rndvelocity[i] = pars[i].rndVelocity;

     scaleBackUp = new Vector3[pars.length];
     scaleBackUp[i] = pars[i].transform.localScale;


     pars[i].minSize = minsize[i] * scale;
     pars[i].maxSize = maxsize[i] * scale;

     pars[i].worldVelocity = worldvelocity[i] * scale;
     pars[i].localVelocity = localvelocity[i] * scale;
     pars[i].rndVelocity = rndvelocity[i] * scale;

     pars[i].transform.localScale = scaleBackUp[i] * scale;
 }

}

Comment

People who like this

0 Show 7 · 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 Statement · Dec 10, 2010 at 12:12 AM 0
Share

Instead of posting another reply, edit your question instead.

avatar image Statement · Dec 10, 2010 at 12:17 AM 0
Share

... And I would write back. I get perfect messages from unityAnswers system that someone would reply to any of my posts :) So do not be afraid I notice you less if you post a comment in my answer.

avatar image 3dDude · Dec 10, 2010 at 12:51 AM 0
Share

yeah sorry. I have noticed that it only allows 8 comments and there are 7 now so if I had commented then how would you have written back?

thanks for the code cleanup I am kind of unorganized hehe

updated the question

anyway thanks for the code clean up and

avatar image 3dDude · Dec 10, 2010 at 12:52 AM 0
Share

ok that was weird it cut of what I was going to say...

I updated the question.

avatar image Statement · Dec 10, 2010 at 01:29 AM 0
Share

Huh? It still doesn't work for you? have you seen my update? I went ahead and tried it myself. I create a game object, add your Scale script, and add a particle emitter to it. I set scale to some value, say 2 and press update. I see the many arrays are populated. I make a prefab out of it. I delete it and add it back to the scene and all values are there, as expected. I think you're doing something way different than what I assume you were doing with this script unless you haven't tried the new version.

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

I want to serialize an array of custom classes. Is it possible? 1 Answer

A "Serialization" bug? 1 Answer

Why is NativeArray always shorter than the number of pixels in the texture? 1 Answer

Dictionary of Dictionaries - How do I store this and assemble it at runtime? 3 Answers

Private Array in custom Class behaving Public -1 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