x


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!

more ▼

asked Dec 09, 2010 at 02:08 PM

3dDude gravatar image

3dDude
2.8k 182 151 178

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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;
    }
}
more ▼

answered Dec 09, 2010 at 05:43 PM

Statement gravatar image

Statement
25.8k 76 108 237

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?

Dec 09, 2010 at 06:30 PM 3dDude

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

Dec 09, 2010 at 06:31 PM 3dDude

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[];

Dec 09, 2010 at 09:24 PM Statement

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

Dec 09, 2010 at 10:29 PM 3dDude

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

Dec 09, 2010 at 10:33 PM Statement
(comments are locked)
10|3000 characters needed characters left

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;
    }
}
more ▼

answered Dec 09, 2010 at 11:46 PM

3dDude gravatar image

3dDude
2.8k 182 151 178

Instead of posting another reply, edit your question instead.

Dec 10, 2010 at 12:12 AM Statement

... 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.

Dec 10, 2010 at 12:17 AM Statement

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

Dec 10, 2010 at 12:51 AM 3dDude

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

I updated the question.

Dec 10, 2010 at 12:52 AM 3dDude

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.

Dec 10, 2010 at 01:29 AM Statement
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2039
x361
x51

asked: Dec 09, 2010 at 02:08 PM

Seen: 2019 times

Last Updated: Dec 10, 2010 at 12:34 AM