Search Unity

Bug Unity crashes while trying a weird way to use arrays

Discussion in 'Linux' started by RedFx, Aug 5, 2016.

  1. RedFx

    RedFx

    Joined:
    Jun 21, 2016
    Posts:
    4
    This post is a bit long, so please bear with me. :D

    I am trying to cast a object into an array but unity crashes without prompt after hitting the play button.
    This could be a nonlinux related issue, however, at the moment I have no way to test it on Windows.

    Command Class snippet:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public abstract class Command {
    6.  
    7.    public Pawn invoker{ get; protected set;} //pawn that is using command
    8.    public Pawn[] targets { get; protected set;}
    9.    public bool auto { get; protected set;}
    10.    public bool validTarget = false; // False = enemies,  true= allies
    11.  
    12.    public Command(Pawn invoker,Pawn[] targets, bool auto) {
    13.      this.invoker = invoker;
    14.      this.targets = targets;
    15.      this.auto = auto;
    16.    }
    17. }
    18.  
    Attack Class snippet:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class Attack : Command {
    5.  
    6.    public const int BASE_HITCHANCE = 200;
    7.  
    8.    public Attack(Pawn invoker, Pawn[] targets, bool auto)
    9.      : base (invoker, targets, auto){
    10.      //animation
    11.      name = "Attack";
    12.      initiative = 30; //standard initiative value
    13.    }
    14. }
    15.  
    Pawn class snippet(Info: Pawns are player-controlled actors in my battle-system):
    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public abstract class Pawn{
    5.  
    6.    public string name { get; protected set;}
    7.    public Command command; //battle current command
    8.  
    9. public void SetCommand (Command c){
    10.      if (c.auto == false) {
    11.        if (command != null)
    12.          command.Reset ();
    13.        this.command = c;
    14.        if (command != null)
    15.          command.Start ();
    16.      }
    17.    }
    18.  
    19.    public override string ToString() {
    20.      return name;
    21.    }
    22.  
    23.    public Pawn[] ToArray() {
    24.      Pawn[] array = new Pawn[1];
    25.      array[0] = this;
    26.      return array;
    27.    }
    28. }
    29.  
    Battler stub:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Battler : Pawn {
    6.  
    7.    (..)
    8. }
    9.  

    I am trying to set the command of a Pawn to Attack with this in my controller:
    Code (csharp):
    1.  
    2. Pawn[] PawnArray = new Pawn[3];
    3. for (int x = 0; x < 3; x++) {
    4.        PawnArray[x] = new Battler();
    5.      }
    6.  
    7. PawnArray[0].SetCommand(new Attack(PawnArray[0],target.ToArray(),false));
    8.  
    The ToArray method is the relevant part where it breaks. I tried nearly everything now but nothing works.
    I also looked into coding another constructor for Attack to prevent this casting and handle single targets directly, however, I wasn't able to due to an ambiguity-error with the base of the command constructor.
    I someone knows a better way to do this, please let me know. I am still in the beginning with c# and stuck on this for days :(

    EDIT: Added Battler Stub and fixed typos to make it easier to understand.
     
    Last edited: Aug 5, 2016
  2. Tak

    Tak

    Joined:
    Mar 8, 2010
    Posts:
    1,001
    Can you send me your project?
     
  3. RedFx

    RedFx

    Joined:
    Jun 21, 2016
    Posts:
    4
    Sorry for not responding earlier,

    However, after I tested my project on my friends Windows Pc, It gave me a stack overflow error due to my incorrect use of autoproperties.

    It seems that in this case the Linux editor process crashes before even showing this error or starting the BugReporter.

    If you still want my project, send me a PN.

    Regards, RedFx