values in prefab script changing AFTER stopping game ?!?!

I have a really simple script:

#pragma strict

var points:Vector2[];

function generateComponent (width:int, pointsArray:Array, offset:Vector2, stretch:float):float {
        var pointss:Vector2[] = points;
        for(var i=0; i<pointss.Length; i++){
            pointss[i].x *= width * stretch;
            pointss[i].x += offset.x;
            pointss[i].y *= width;
            pointss[i].y += offset.y;
           
            pointsArray.Add(pointss[i]);
        }
       
        return pointss[pointss.Length-1].y;
}

All it does is contain some x-y coordinates, which i can set up in the inspector. The prefab is dragged onto another script which calls it some times to insert these coordinates into a larger array.

The strange thing is: When im running the game, and then stop it, the values which i have set up in the inspector are modified. How is that even possible? Regardless of how messed up anything might be, stuff should reset once i stop running the game, shouldnt it? But this one is modifying my actual original pre-fab object, destroying my original data i entered.

Also, it shouldnt be very messed up, because i have a similar script, with only difference being, that i have set up a number of x-y coordinates inside the actual script, and it works just fine. Only difference between that one, and this one would be, that i want this to be inspector-modifiable for convenience.

This is not necessarily the case with anything that modifies assets. If your in-scene object has a reference to this prefab, and it changes the values in this array, then I believe those changes will persist.

And since this is an array, my spider-sense suggests to me that you may be doing something like… copy the array to a variable on your in-scene object, changing values there, and expecting the original array to be unchanged. This is not the case! Arrays are passed by reference, so: (it’s C#, but you should get the gist)

Vector2[] firstArray = new Vector2[]{new Vector2(0f, 0f), new Vector2(5f, 0f)};
Vector2[] secondArray = firstArray;
secondArray[1] = new Vector2(999f, 0f);
Debug.Log("firstArray[1]: "+firstArray[1]);

This will actually output 999,0. Modifying the second array changed the first one, because they’re both pointing to the same spot in memory. You can solve that particular issue by cloning the array (making a “deep copy”).

Vector2[] firstArray = new Vector2[]{new Vector2(0f, 0f), new Vector2(5f, 0f)};
Vector2[] secondArray = (Vector2[])firstArray.Clone();
secondArray[1] = new Vector2(999f, 0f);
Debug.Log("firstArray[1]: "+firstArray[1]);

That will now output 5,0 as expected. (Sorry, I don’t know offhand the syntax for this in Javascript, but it shouldn’t be too different)

What is the code that calls on the prefab and pulls its values? Are you operating on the prefab itself or an instance of the prefab? Modifying actual instanced objects is reset automatically after you stop running the game, but I believe if you directly modify a prefab - or any actual asset in your project, those changes remain.

I solved the problem. Yes, i was operating on the original prefab, and yes, i figured out that if i assign the original array to another variable, it gets assigned as a reference.

So i did something like StarManta suggested, filling up second array value by value, so the original remains untouched.

I did not know that it was possible to persistently modify a prefab by running the game. Could this be used to save persistent data, such as highscore and such, or is this working differently in a compiled game?

I’ve never tried it, buy my guess would be that it modifies the prefab’s values while the game is running, but it’s highly unlikely this is saved between sessions.

It’s definitely possible in the editor. One of the earlier games I programmed modified Material and prefab values directly on disk, and it would cause what we called “svn spam” when committing.

I have no idea if that would work on device. I certainly don’t suggest it. (Properly use a save file or PlayerPrefs!) I’ve learned to code so that materials and prefabs never change on disk.