Search Unity

Initializing array syntax/logic problem?

Discussion in 'Scripting' started by giraffe1, Apr 4, 2019.

  1. giraffe1

    giraffe1

    Joined:
    Nov 1, 2014
    Posts:
    179
    Hi,

    Through the inspector I can set the size of the number of waypoints per path. How can I initialize it through code. I am stuck on how get the AddWaypoints() to change the element size of the waypoint array per path.

    Ultimately I am trying to make an editor script that allows me to click in the scene view and generate way points that will be fed into the way points array of each path. This is triggered by a button in the inspector. And it happens with play mode OFF.

    Any help is appreciated. These are the 2 classes:

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4. [Serializable]
    5. public class PatrolPath
    6. {
    7.     public Transform[] waypoints;
    8. }
    The other 1 is:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class UnitSpawner : MonoBehaviour
    4. {
    5.     public PatrolPath[] paths;
    6.  
    7.     private int _pathCount = 0;
    8.     private int _waypointCount = 0;
    9.  
    10.     public void CreateNewPath()
    11.     {
    12.         _pathCount++;
    13.  
    14.         // Generate a new path object
    15.         paths = new PatrolPath[_pathCount];
    16.     }
    17.  
    18.     public void AddWaypoints()
    19.     {
    20.         _waypointCount++;
    21.  
    22.         // Waypoint array is size 0 right now
    23.         // Increase array size somehow???
    24.         new paths[_pathCount].wayPoints[_waypointCount];
    25.     }
    26. }
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    1,760
    Code (CSharp):
    1. if (paths == null) paths = new paths[_pathCount];
    2.  
    3. // Index is the path index that you want to modify. Pass this to the method instead of modifying last path
    4. // Also, check if it's in bounds of paths' length.
    5. Transform[] prevArray = paths[index].wayPoints;
    6. Transform[] newArray = new Transform[_waypointCount];
    7.  
    8. // Don't forget to copy the prevArray to the new waypoints
    9. if (prevArray != null && prevArray.Length > 0) {
    10.     // Length is the previous count of elements to copy, so minus one if you've incremented it
    11.     Array.Copy(prevArray, 0, newArray, 0, _waypointCount - 1);
    12. }
    13.  
    14. paths.wayPoints = newArray;
    Should be something like this.

    Note that creating arrays constantly will allocate garbage and sink your performance.
    Just use Lists. There's not much of performance gain of arrays over the lists.

    Code (CSharp):
    1. // In the data
    2. List<Tranform> waypoints;
    3.  
    4. // Then do the add. Just don't forget to null check / initialize the waypoints list.
    5. waypoints.Add(*your_point*);
     
    Last edited: Apr 4, 2019
  3. giraffe1

    giraffe1

    Joined:
    Nov 1, 2014
    Posts:
    179
    I will try that out. Thanks.