Search Unity

Problem with LIST of Gameobjects when running

Discussion in 'Scripting' started by Briek, Nov 21, 2017.

  1. Briek

    Briek

    Joined:
    Oct 11, 2017
    Posts:
    2
    I'm doing a paltform prefab for easy editing, and i've tried instantiating some "pathwaypoints" as gameobjects while still in the editor.
    The problem is: I add this GO to a list in my gamemanager, and they are added correctly, but as soon as I press Play those points are deleted from the list and become null. ¿What am i doing wrong?

    Heres my code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. //using UnityEditor;
    5. using UnityEngine.SceneManagement;
    6.  
    7.  
    8. public enum PlatformUnit { Fija =0, Flexible, Columpio}
    9. public enum PlatformUnitNormalType { ConActivacion=0,SinActivacion }
    10. public enum PlatformUnitNormalTypeOfMovement { Bucle = 0, dePuntoaPunto, BucleCerrado }
    11. [System.Serializable]
    12. public class PlatformType
    13. {
    14.     //Types to choose
    15.     public PlatformUnit unit;
    16.     public PlatformUnitNormalType unitNT;
    17.     public PlatformUnitNormalTypeOfMovement unitNTM;
    18.     public PlatformUnit savedunit;
    19.  
    20.     //General Variables
    21.     public float ReactionTime=1f;
    22.     public GameObject Trigger;
    23.     public float Speed;
    24.     public bool TriggerisStatic;
    25.  
    26.     //Swing variables
    27.     public GameObject ConnectedPlatform=null;
    28.     public bool isUP=true; //Definido por la position del objeto de ruta
    29.     public float FloorDistance=2;
    30.  
    31.     //Normal Variables
    32.    
    33.     public int PathwayPoints=1;
    34.  
    35. }
    36. // addrelativeforce , para que la plataforma arrastre al jugador
    37.  
    38. public class Platform_Script : MonoBehaviour {
    39.  
    40.     public PlatformType platform;
    41.     Platform_Script conPlat;
    42.     GameObject Platform_Waypoint;
    43.     GameObject Platform_Mesh=null;
    44.     public bool triggered = false;
    45.     float Lastime=0;
    46.     Vector3 Endpos, Startpos;
    47.     int PathP,direction=1;
    48.  
    49.     private void CreateWaypoint(Vector3 position)
    50.     {
    51.         Platform_Waypoint = Instantiate(Resources.Load("Platforms/Platform_Waypoint") as GameObject, this.transform);
    52.         Platform_Waypoint.name = "Platform_Waypoint";
    53.         Platform_Waypoint.transform.localPosition = position;
    54.         GameManager_lara.gameM.Waypoints.Add(Platform_Waypoint);
    55.         //platform.Waypoints.Add(Platform_Waypoint);
    56.         //GameObject instance2 = Instantiate(block, obj2.transform.position + (transform.forward * 1.5f), obj.transform.rotation);
    57.     }
    58.  
    59.     private void DestroyWaypoints()
    60.     {
    61.         foreach(GameObject point in GameManager_lara.gameM.Waypoints)
    62.         {
    63.             if(point != this.gameObject) DestroyImmediate(point);
    64.         }
    65.         GameManager_lara.gameM.Waypoints.Clear();
    66.     }
    67.  
    68.  
    69.     void AddWaypoints()
    70.     {
    71.         GameManager_lara.gameM.Waypoints.Add(this.gameObject);
    72.         for (int i = 1; i <= platform.PathwayPoints; i++)
    73.         {
    74.             CreateWaypoint(new Vector3(0, i, 0));
    75.         }
    76.     }
    77.  
    78.  
    79.     private void OnDrawGizmos()
    80.     {
    81.  
    82.         if (Application.isPlaying == false)
    83.         {
    84.             if (Platform_Mesh == null) Platform_Mesh = transform.Find("Platform_Mesh").gameObject;
    85.  
    86.  
    87.             //When changing Types
    88.             if (platform.savedunit != platform.unit)
    89.             {
    90.                 switch (platform.unit)
    91.                 {
    92.                     case PlatformUnit.Columpio:
    93.                         DestroyWaypoints();
    94.                         CreateWaypoint(new Vector3(0, 0, 0));
    95.                         break;
    96.                     case PlatformUnit.Flexible:
    97.                         DestroyWaypoints();
    98.                         AddWaypoints();
    99.                         break;
    100.                     case PlatformUnit.Fija:
    101.                         DestroyWaypoints();
    102.                         break;
    103.                 }
    104.                 platform.savedunit = platform.unit;
    105.             }
    106.  
    107.  
    108.             //When changing options
    109.             if (platform.unit == PlatformUnit.Columpio)
    110.             {
    111.                 Platform_Waypoint = transform.Find("Platform_Waypoint").gameObject;
    112.                 platform.Trigger = transform.Find("Platform_Trigger").gameObject;
    113.                 if (platform.isUP) { Platform_Waypoint.transform.localPosition = new Vector3(0, -platform.FloorDistance, 0); Platform_Mesh.transform.localPosition = new Vector3(0, 0, 0); if (!platform.TriggerisStatic) platform.Trigger.transform.localPosition = new Vector3(0, 0, 0); }
    114.                 else { Platform_Waypoint.transform.localPosition = new Vector3(0, 0, 0); Platform_Mesh.transform.localPosition = new Vector3(0, -platform.FloorDistance, 0); if (!platform.TriggerisStatic) platform.Trigger.transform.localPosition = new Vector3(0, -platform.FloorDistance, 0); }
    115.  
    116.             }
    117.             if (platform.unit == PlatformUnit.Flexible)
    118.             {
    119.                 int count = (GameManager_lara.gameM.Waypoints.Capacity - 1);
    120.                 if (platform.PathwayPoints != count )
    121.                 { DestroyWaypoints(); AddWaypoints(); }
    122.             }
    123.         }
    124.     }
    125.  
    126.     List<GameObject> Waypnts;
    127.  
    128.     void Start()
    129.     {
    130.         platform.Trigger = transform.Find("Platform_Trigger").gameObject;
    131.         Platform_Mesh = transform.Find("Platform_Mesh").gameObject;
    132.         if (platform.unit == PlatformUnit.Columpio) conPlat = platform.ConnectedPlatform.GetComponent<Platform_Script>();
    133.         if (platform.unitNT == PlatformUnitNormalType.SinActivacion) triggered = true;
    134.         Waypnts = GameManager_lara.gameM.Waypoints;
    135.     }
    136.  
    137.     private void Update()
    138.     {
    139.         if (triggered) {
    140.             switch (platform.unit)
    141.             {
    142.                 case PlatformUnit.Columpio:
    143.                      MoveColumpio();
    144.                     break;
    145.                 case PlatformUnit.Flexible:
    146.                     MoveFlexible();
    147.                     break;
    148.                 case PlatformUnit.Fija:
    149.                     break;
    150.             }
    151.         }
    152.     }
    153.  
    154.     private void OnTriggerEnter(Collider other)
    155.     {
    156.         if (other.tag == "Player" && !triggered && platform.isUP) {
    157.             if (platform.unit == PlatformUnit.Columpio)
    158.                 { triggered = true; conPlat.triggered = true; }
    159.             if (platform.unit == PlatformUnit.Flexible)
    160.                 { triggered = true;  PathP = 0; Endpos = Waypnts[PathP].transform.position; }
    161.         }
    162.     }
    163.     private void OnTriggerStay(Collider other)
    164.     {
    165.         if (platform.unit == PlatformUnit.Columpio)
    166.             if (other.tag == "Player" && !platform.isUP && !triggered) SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    167.     }
    168.  
    169.     private void MoveColumpio()
    170.     {
    171.         if (Lastime == 0) Lastime = Time.time;
    172.         float dif = Time.time - Lastime;
    173.         if (dif > platform.ReactionTime)
    174.         {
    175.             if (platform.isUP)
    176.             {
    177.                 if (Platform_Mesh.transform.localPosition != new Vector3(0,-platform.FloorDistance, 0))
    178.                 { Platform_Mesh.transform.localPosition = Vector3.MoveTowards(Platform_Mesh.transform.localPosition, new Vector3(0, -platform.FloorDistance, 0), platform.Speed * Time.deltaTime); }
    179.                 else
    180.                 {
    181.                     platform.isUP = false;
    182.                     triggered = false;
    183.                     Platform_Waypoint.transform.localPosition = new Vector3(0, 0, 0);
    184.                     Lastime = 0;
    185.                 }
    186.             }
    187.             else
    188.             {
    189.                 if (Platform_Mesh.transform.localPosition != new Vector3(0, 0, 0))
    190.                 { Platform_Mesh.transform.localPosition = Vector3.MoveTowards(Platform_Mesh.transform.localPosition, new Vector3(0, 0, 0), platform.Speed * Time.deltaTime); }
    191.                 else
    192.                 {
    193.                     platform.isUP = true;
    194.                     triggered = false;
    195.                     Platform_Waypoint.transform.localPosition = new Vector3(0,-platform.FloorDistance, 0);
    196.                     Lastime = 0;
    197.                 }
    198.             }
    199.             if(!platform.TriggerisStatic) platform.Trigger.transform.position = Platform_Mesh.transform.position;
    200.         }
    201.     }
    202.  
    203.     private void MoveFlexible()
    204.     {
    205.         Vector3 platpos = Platform_Mesh.transform.position;
    206.  
    207.         //Movimiento
    208.         if (platpos != Endpos)
    209.         {
    210.             Vector3 newpos = Vector3.MoveTowards(Startpos, Endpos, platform.Speed * Time.deltaTime);
    211.             Platform_Mesh.transform.position = newpos;
    212.             /*Vector3 currentpos = Vector3.Lerp(Startpos, Endpos, Mathf.Cos(Time.time / platform.Speed * Mathf.PI * 2) * -5f + .5f);
    213.             Rigidbody rb = Platform_Mesh.GetComponent<Rigidbody>();
    214.             rb.MovePosition(currentpos);*/
    215.  
    216.         }
    217.         else
    218.         {
    219.             Startpos = platpos;
    220.             PathP= PathP+ (1*direction);
    221.             Endpos = Waypnts[PathP].transform.position;
    222.         }
    223.  
    224.         switch (platform.unitNTM)
    225.         {
    226.             case PlatformUnitNormalTypeOfMovement.Bucle:
    227.                 if (PathP >= Waypnts.Capacity) direction = -1;
    228.                 if (direction == -1 && PathP == 0) direction = 1;
    229.                 break;
    230.             case PlatformUnitNormalTypeOfMovement.BucleCerrado:
    231.                 if (PathP >= Waypnts.Capacity) PathP = 0;
    232.                 break;
    233.             case PlatformUnitNormalTypeOfMovement.dePuntoaPunto:
    234.                 if (PathP >= Waypnts.Capacity) { triggered = false; direction = -1; }
    235.                 if (direction == -1 && PathP == 0) { triggered = false; direction = 1; }
    236.                 break;
    237.         }
    238.     }
    239. }
     
  2. Errorsatz

    Errorsatz

    Joined:
    Aug 8, 2012
    Posts:
    555
    I'm assuming Waypnts is the list?
    In Start, you're replacing it with GameManager_lara.gameM.Waypoints, which is going to overwrite anything you had in there before.
     
  3. Briek

    Briek

    Joined:
    Oct 11, 2017
    Posts:
    2
    The list is in the game manager and it's
    GameManager_lara.gameM.Waypoints
    What i'm doing is only copying that list onto another one in that script when pressing play (so i dont have to keep writing all that long code).

    The problems is that only those waypoints (wich are instanced prefabs) are "deleted" from the list, if i add another item to the list wich is not instanced in the scene, it stays.
    And when i check the GO list in the gamemanager Inspector (nothing to do with the list in the script), they dissapear anyways when pressing play.