- Home /
Unity crashing because of added object
So i have the most recent update of unity and it was running fine. Unity was crashing before for no reason so i deleted it off of my computer. Now its re-installed and actually working, i was able to rebuild my game and re-import my code. When i went to test the simple things it was working but when i started adding the in complex buildings unity just crashes. Its not because the graphical power was to much, its just a few blocks, but the code was a lot more complex. I just found out that it's the code that crashing the program theres also a stackoverflowexception error with it that i can't seem to find the root cause of in the program. Can you help me find out why this code is crashing the program.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ArcheryTowerScript : MonoBehaviour {
public GameObject Arrow;
public Transform myTransform;
public List<Transform> EnemyArray = new List<Transform>();
public int value = 0;
public int value1 = 0;
public Transform Cube;
ArcheryTowerScript call = new ArcheryTowerScript();
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(EnemyArray.Count > 0)
{
value = 1;
call.arrowCreation();
}
if(EnemyArray.Count <= 0)
{
value = 0;
}
}
IEnumerator OnTriggerEnter(Collider collision)
{
yield return new WaitForSeconds(0.1f);
if(collision.transform.tag == "Enemy")
{
EnemyArray.Add(Cube);
}
}
void OnTriggerExit(Collider collision)
{
if(collision.transform.tag == "Enemy")
{
EnemyArray.RemoveAt(0);
}
}
public Transform recentEnemy()
{
return EnemyArray[0];
}
public void setCube(Transform myCube)
{
Cube = myCube;
}
public IEnumerator arrowCreation()
{
value1 = 1;
if(value == 1)
{
for(int x = 0; x < 10; x++)
{
Instantiate(Arrow, new Vector3(myTransform.position.x,myTransform.position.y,myTransform.position.z), Quaternion.identity);
yield return new WaitForSeconds(1);
x = 0;
}
}
}
}
Possible corrupted asset? $$anonymous$$ight explain crash-on-import or crash-on-adding-to-scene. What kind of asset? A model? What file type? Can you open it in your 3D editing application?
I was able to take the object out of the scene by deleting the script out of the project. So im messing around with it now, its the script that is causing unity to crash for some reason. I've deleted the script and rewrote it from scratch but thats not the fixing the problem. There is a error in the console saying "StackOverflowExeception"
Answer by robertbu · Nov 01, 2014 at 04:12 PM
This script is the problem. In particular this line:
ArcheryTowerScript call = new ArcheryTowerScript();
You shouldn't create MonoBehaviours through the 'new' operator, but no matter how you create them, this line would not work (inside an ArcheryTowerScript). Each 'new' ArcheryTowerScript object is in turn creating a new ArcheryTowerScript object in an endless chain until you crash the stack.
Okay thanks so im making that object so i can call a method within my own class. Everything i seem to be doing doesn't seem to work and i dont want to make the methods static. How would i do this?
I don't understand why you did what you did, so I'm not sure I understand what you are attempt to do here. But just on the face of it:
Any script derived from '$$anonymous$$onoBehaviour' should be attached to a game object. So either this script is dragged and dropped on an existing scene object, attached to a prefab which is Instantiated() or is added at runtime using AddComponent().
Coroutines should be executed using StartCoroutine()
- Your current code repeatedly executes 'arrowCreaction' even while it is running. This will stack up coroutines.
At the start and end of 'arrowCreation' set a class instance variable:
creatingArrows = true;
...and at the end:
creatingArrows = false;
Then your call code on lines 23 - 28 become:
if(!creatingArrows && EnemyArray.Count > 0)
{
value = 1;
StartCoroutine(arrowCreation());
}