Assigning an array of arrays from a scriptable object

I have a WaveSpawner script that spawns an array of waves. Each wave has an array of enemies.
This works perfectly when i assign everything in the inspector.

What I’m trying to do is populate the arrays in the waveSpawner Gameobject with a scriptable object that is referenced on another object.

The object StopPoint is what holds the scriptable object.

public class WaveSpawner : MonoBehaviour
{
    [SerializeField] private Wave[] waveArray;
    public GameObject currentStopPoint;

    void Update()
    {
        if(currentStopPoint != null)
        {
            if (currentStopPoint.GetComponent<StopPoint>().isActive)
            {
                if (state == State.Idle)
                {
                    //currentStopPoint.GetComponent<StopPoint>().wavesArray.CopyTo(waveArray, 3);
                    //System.Array.Copy(currentStopPoint.GetComponent<StopPoint>().wave, waveArray, 2);
                    //waveArray.Copy(currentStopPoint.GetComponent<StopPoint>().wave, waveArray);
                    WaveObject.Waves[] wavesArray = currentStopPoint.GetComponent<StopPoint>().wave.wavesArray;
                    //waveArray = wavesArray;
                    //waveArray.Copy(wavesArray, waveArray, 2);
                    //waveArray = System.Array.Copy(wavesArray, waveArray, 3);
                    //waveArray = waveArray.Copy(wavesArray, waveArray, wavesArray.Length);
                    waveArray = wavesArray.CopyTo(waveArray, wavesArray.Length);
                    waveArray = waveArray.Copy(wavesArray, waveArray, wavesArray.Length);
                    //waveArray = System.Array.Copy(currentStopPoint.GetComponent<StopPoint>().wave.wavesArray, waveArray, currentStopPoint.GetComponent<StopPoint>().wave.wavesArray.Length);
                    StartSpawn();
                }
            }
        }

    public void StartSpawn()
    {
        //wave.SpawnEnemies();
        state = State.Active;
    }

    /// <summary>
    ///
    /// </summary>
    [System.Serializable]
    public class Wave
    {
        [SerializeField] private Enemy[] enemiesArray;
        [SerializeField] private float timer;
       // public Transform spawnPoint;
        public static int enemyCount;
        
}

Scriptable object

[CreateAssetMenu]
public class WaveObject : ScriptableObject
{
    public Waves[] wavesArray;


    [System.Serializable]
    public class Waves
    {
        public Enemy[] enemiesArray;
        public float timer;
       // public Transform spawnPoint;

       
    }

You left out the part about how that is not happening with the code above.

Generally it doesn’t matter what you’re doing with another script: you need a reference to its instance, then you need a way to retrieve what you want from it, or tell it what you want to do.

You can make fields public and just get/set them, or you can make a nice wrapper function named usefully such as “GetAvailableThingies();” for example.

1 Like

I want to assign the array from the waveSpawner to equal the array in the scriptable object. I cant seem to get it to work.
These are all the failed attempts i tried:

if (state == State.Idle)
                {
                    //currentStopPoint.GetComponent<StopPoint>().wavesArray.CopyTo(waveArray, 3);
                    //System.Array.Copy(currentStopPoint.GetComponent<StopPoint>().wave, waveArray, 2);
                    //waveArray.Copy(currentStopPoint.GetComponent<StopPoint>().wave, waveArray);
                    WaveObject.Waves[] wavesArray = currentStopPoint.GetComponent<StopPoint>().wave.wavesArray;
                    //waveArray = wavesArray;
                    //waveArray.Copy(wavesArray, waveArray, 2);
                    //waveArray = System.Array.Copy(wavesArray, waveArray, 3);
                    //waveArray = waveArray.Copy(wavesArray, waveArray, wavesArray.Length);
                   // waveArray = wavesArray.CopyTo(waveArray, wavesArray.Length);
                    //waveArray = waveArray.Copy(wavesArray, waveArray, wavesArray.Length);
                    //waveArray = System.Array.Copy(currentStopPoint.GetComponent<StopPoint>().wave.wavesArray, waveArray, currentStopPoint.GetComponent<StopPoint>().wave.wavesArray.Length);
                    StartSpawn();
                }

Line 6 above just points over at the array and says “That’s me too!” as arrays are reference types. If someone changes elements within that other array, this local copy will see those changes too.

ALSO: line 6 is making a fresh local variable, you are NOT changing the class level variable. That might be your entire issue??

Arrays themselves cannot be changed, so if someone reassigns the other array, this copy will remain whatever it was before then.

This is a very shallow copy, or simply a reference.

If you want to make a deeper copy you can create the array of the right size, then either copy it over yourself one by one, or use this to copy it over:

This would make a duplicate array, filled in with the same items.

Before trying to get stuff from other scripts, I recommend working through the examples above to understand the varying depths of copy. Don’t boggle yourself up with referring to other scripts. Make everything in one script, “pretending” that you have a reference to the foreign array, until you feel comfortable making copies.

The next level deeper of copying is to make clone-copies of the individual items, but I doubt you want that.