- Home /
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!
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 < 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;
}
}
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?
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 $$anonymous$$size is storing floats can you do private var $$anonymous$$size : float[];
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
See my update. You need to allocate the array with the 'new' keyword before using it.
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:
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;
}
}
Ins$$anonymous$$d of posting another reply, edit your question ins$$anonymous$$d.
... 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.
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
ok that was weird it cut of what I was going to say...
I updated the question.
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.
Your answer
Follow this Question
Related Questions
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
Serialize Variables from Items in an Array 1 Answer
Can I define initial variable for serialized array in the inspector? 6 Answers
Classes that contain an array of its own type do not appear in the inspector 2 Answers