I wrote a basic script for Unity in C#.
Everything was going well but during my latest tests, Unity freezed like if I launched an infinite loop. I didn't understand what was wrong so I gave my code to a friend to see if he could make it work. He got it working but after a while, there is an error about pointers. He gave it back to me and it still freeze my Unity. If I lauch the Debug, everything seem to be all right.
Do you understand what's wrong with this code? Does-it work on your computer? Should-I buy a new computer?
There is my code. To use it, include Decision and DigestiveSystem in an object.
Decision's structure might seem weird but I want it to use a lot of other scripts without crashing if one is missing.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Decisions : MonoBehaviour
{
private List Action;
private List Want;
private bool exist_DS;
private DigestiveSystem DS ;
// Use this for initialization
void Start ()
{
/*if (DS == null)
Debug.Log ("null");
else
Debug.Log (DS.IntestinesPercentage);*/
Action = new List();
Want = new List();
DS = null;
exist_DS = false;
}
private void DSReady()
{
DS = GetComponent ();
}
private void DSInit()
{
Action.Add ("Eat");
Action.Add ("Evacuate");
Want.Add (1 - DS.StomachPercentage);
Want.Add (DS.IntestinesPercentage);
exist_DS = true;
}
// Update is called once per frame
void Update ()
{
if (DS == null)
Debug.Log ("null");
else
Debug.Log ("non-null");
if (DS != null && exist_DS == false)
DSInit ();
/*if (exist_DS)
{
Debug.Log ("SP "+DS.StomachPercentage);
Debug.Log ("IP "+DS.IntestinesPercentage);
}*/
int Maximum = GetWantMaxPos ();
/*Debug.Log ("A.C "+Action.Count);
Debug.Log ("W.C "+Want.Count);*/
Debug.Log ("max "+Maximum);
foreach(double d in Want)
{
Debug.Log("WANT: "+d);
}
/*if(Maximum!=-1)
{
switch (Action[Maximum])
{
case "eat":
Debug.Log("Eat");
GetComponent().Eat (1);
break;
case "evacuate":
Debug.Log("Evacuate");
GetComponent().Evacuate();
break;
}
}*/
}
private int GetWantMaxPos()
{
int max = 0;
for (int pos=0; posWant[max])
pos=max;
}
if (Want.Count == 0)
max = -1;
return max;
}
}
----------
using UnityEngine;
using System.Collections;
public class DigestiveSystem:MonoBehaviour
{
private Organ stomach;
private Organ intestines;
private bool IsInit;
public double StomachPercentage
{
get{ return stomach.Percentage;}
}
public double IntestinesPercentage
{
get{ return intestines.Percentage;}
}
// Use this for initialization
void Start ()
{
stomach = new Organ (90,100,1);
intestines = new Organ (30,100,1);
IsInit = false;
}
// Update is called once per frame
void Update ()
{
if (IsInit == false)
{
GetComponent().SendMessage("DSReady");
IsInit=true;
}
intestines.Add(stomach.Remove (Time.deltaTime));
/*Debug.Log (Time.timeSinceLevelLoad);
Debug.Log (stomach.Percentage);
Debug.Log (intestines.Percentage);*/
}
public void Eat( double Qt)
{
stomach.Add (Qt);
}
public double Evacuate()
{
return intestines.Remove (Time.deltaTime);
}
}
----------
using UnityEngine;
using System.Collections;
public class Organ
{
private double Quantity;
private double Max;
private double Speed;
public double Percentage
{
get{ return Quantity / Max;}
}
public Organ(double start_Quantity, double start_Max, double start_Speed)
{
Quantity = start_Quantity;
Max = start_Max;
Speed = start_Speed;
}
public void Add(double Qt)
{
Quantity += Qt;
}
public double Remove(double time)
{
double Qt = Speed * time;
if (Quantity < Qt)
Qt = Quantity;
Quantity -= Qt;
return Qt;
}
}
↧