#unity keeps crashing

1 messages · Page 1 of 1 (latest)

gusty cargo
#

so here, I've got a little program having a couple of scripts, some prefabs, 2 animations, 3 materials and a physics material.
Everything seems completely fine with me but the problem happens whenever I try to run the program. Whenever I hit play, unity crashes. It ain't some message like "unity stopped responding", but I wont do anything and just, freeze. I'd have to do to task manager again and again to end the task. Does somebody experiance something similar, or does someone have any suggestions?

earnest delta
#

#1007606464677949480 message

gusty cargo
#

huh?

#

not there

onyx skiff
#

sounds like an infinite loop is somewhere in your scripts

#

Unity crashes when I try to run my game!
Crashes on start usually are an indicator of an infinite loop happening somewhere in your code.
Any kind of loop (for, while, foreach) as well as recursive method calls will try executing within one frame*. Because of that, if any given loop has no or an insufficient exit condition, it will never end - causing Unity to crash.
The solution is to double-check any recent code changes you made, figure out where you might be creating an infinite loop, and fixing that.

  • The exception to this behaviour are loops within coroutines, which contain one or multiple yield return statements.
#

this is from the FAQ that was linked previously

gusty cargo
#

there is indeed a while loop which, in my perspective, is the cause of unity crashing repeatedly:

while (Player.transform.position.z < ObstacleParent.transform.position.z)
In this loop, the ObstacleParent is a static gameobject which is initiated from a prefab while the Player is... well the player, who keeps going forward...

#

however, i need this while loop as this command is called in a function other then Update or FixedUpdate and i need this to work continuesly as long as the position of tee player is lower than that of the obstacle\

quasi obsidian
gusty cargo
#

but i cant use this command in the update method

#

that's because the update function is the main one from which i am spawning different types of obstacles

#
        if(sec > 2f && SpawnIt == true)
        {
            /*c = Random.Range(1, 4);*/
            c = 4;

            switch (c)
            {
                /*case 1:
                    StaticSpaceObstacleSpawn();
                    p += 150;
                    break;

                case 2:
                    SingleSlidingObstacleSpawn();
                    p += 150;
                    break;

                case 3:
                    /*SlidingObstacleSpawn();
                    p += 25;
                    SlidingObstacleSpawn();
                    p += 25;
                    SlidingObstacleSpawn();
                    p += 150;*//*
                    MultipleSlidingObstacleSpawn();
                    p += 150;
                    break;*/

                case 4:
                    ProjectileObstacleSpawn();
                    p += 150;
                    break;
                /*case 3:
                    FallingObstacleSpawn();
                    break;*/
            }

            if(DeleteList.Count > 2)
            {
                SpawnIt = false;
            }```
#

the different types of obstacles are spawned according to the switch

#

i used the update function to write these functions before but that seems a bit too 'jumbled'

earnest delta
#

A method appearing cluttered is not a reason to use a different approach.
Since you obviously know of the concept of methods, just use them. If one seems jumbled, put parts of it into another one. This holds true for any method, including Update.

I strongly assume that the while loop you mention is neither inside a coroutine, nor moves the player itself.
Becaude of that, its condition will never become false - thus creating an infinite loop. (If you don't understand why, read the FAQ text again).

gusty cargo
#

no

#

the player has another script wshich makes it move forward

#

the game is a little like subwar surfers

#

the playuer continuesly moves forward and the obstacles are spawned and despawned according to that

#

is there really no other way to overcome this ?

earnest delta
#

Unity is single-threaded.
If you create a loop - any loop - once it starts, executing this loop is everything the game will be doing until the loop ends, because code execution literally cannot move on until the loop has released control flow.

gusty cargo
#

but the loop can end if the player's position is more than that of the obstacle?

#

, right?

#

also, i'd need to use the while loop even if it were in the update function

#

i'll send the complete code and then youll understand my login

#

what i need this loop for is making a continues flow of obstacles as long as the obstacle is behind it

#

if i were to use a for loop, it wouldn't be indefinate

earnest delta
# gusty cargo but the loop can end if the player's position is more than that of the obstacle?

It could.
But unless you are moving the player (or obstacle) from inside the loop, that will never happen, because they will never again get a chance to move before the loop ends. Which will not happen, because the only thing that could cause the loop to end, would be the position changing. Which can't happen because the loop does not move objects, causing positions to stay the same until the loop ends. Which can't happen until objects move. Which can't happen until the loop ends. Which can't happen... [ad infinitum]

Do you see the issue?

gusty cargo
#

so, could i use a variable to save the current position of the player at the end of the loop, which i'd be checking in the condition?

#

like

{... float PlayerPosition = player.transform.position.z;}
#

that way, i'd be checking the player's position every time the loop ruins, right?

earnest delta
gusty cargo
#

oh

earnest delta
gusty cargo
#

yes thats true

#

but there is a liunit to the for loop

#

yes, i could use i-- in the end of the loop to make it indefinate, but thats basically the same as the while lloop

#

so, that wouldn't help me either

earnest delta
# gusty cargo

this is displayed to me as a text file.
if you want to send long code, please use a paste site.

gusty cargo
#

hm?
sorry, but i dont know that much

#

i just copied my code in the editor and p[asted it here

#

i copied the code to a website and then saved it to make a link.
Is it alrigght?

#

sorry for the typos.I dont have something like grammarly and i aint very good at typing

#

hey

earnest delta
#

I may have a look later.
It's difficult to read that code on phone.

earnest delta
#

This code is incredibly confusing. Mostly because your variable naming is not just bad, but pretty much non-existent.
I have no idea what t, p, n, i, c, s, g and m are supposed to do. And really, if you code like this it's a safe way to introduce unwanted behaviour, simply by accidentally using the wrong variable in the wrong place because you happened to forget what it does.
Of course you shouldn't make names that span an entire page width. But one singular character is way too little.

gusty cargo
#

yeah, thats also a problem

#

the fact is that those are the variables i needed when thinking about custonizing the game

#

in fact, i have already made this game once but at that time, i didnt make an automatic spawner.
insted, i just made the prefabs and made the levels myself with them

#

this time, i made a basic prototype in the beginning and then edited the script according to the ideas i got from time to time, so whenever I'd get short of a variable, id just create a new one to use without thinking much about it
Once again, sorry for the inconvenience.

#

sorry brother...
I am a noob in unity and can't be of much help

earnest delta
#

Do not try to bump your questions in other threads.

gusty cargo
#

so, did you find anything?

#

or should i modify the script a bit?

#

also, could i contact you a few hours later, say around 10?

#

cuz its pretty late where i'm staying and my mom would be mad if she found out im up at this hour]

earnest delta
earnest delta
gusty cargo
#

i meant 10 hrs later

#

cuz i live in india and its around 00:00 here

earnest delta
#

I won't be awake in 10 hours. But I may get back to you later after you post it.

gusty cargo
#

i dont have any other problems

#

it would be great if i could get some help in this one

#

thanks

gusty cargo
#

hey

#

did you find anything?

gusty cargo
earnest delta
#

Well you still have the same infinite loop that we already talked about in your code.
Variable naming is a little better, but still lacking; I for one have no idea what the L prefix is for.

The main issue with the loop seems to be that you are using your obstacle spawner to implement logic that should be a part of the obstacle itself (namely spawning projectiles of some sort).
I would suggest that you make a script responsible for spawning projectiles from its Update method. Always separate concerns; Don't try to make one class that does everything. I know that spawning objects and spawning projectiles might look like it's the same thing, but it happens in different contexts.
And if you do want to keep the logic in your spawner script, you'll need a coroutine. Otherwise you willl inevitably run into the issue of the infinite loop and crash your game.

Also, make sure you really understand loops. Because doing this cs while (Player.transform.position.z < ObsP.transform.position.z + 5) { ... if( Player.transform.position.z >= ObsP.transform.position.z ) { break; } }makes little sense, logically.

gusty cargo
#

i've deleted the unnecessary variables and L in the variables stands for 'launch'.

Also, you're right about the condition loop, I just didn't know what to do and felt that it was worth a try, anyways so it was just a meaningless line of code

#

Linterval1 means like the interval btw every bullet an M26 rifle fires
Linterval2 means the interval btw every 3-shot burst of the rifle
Like this ... ... ...

#

just that instead of these dots or the bullets, there will be blocks.

#

my computer teacher suggested my to use InvokeRepeating but that didn't have any use in this context

earnest delta
gusty cargo
#

i know that

#

thats why i didnt use it when i rewrote the code this morning

#

but i still dont know any way to make the code run as intended

#

i do understand what i meant by the m16 example, right?

#

cuz' i know i was vague, but i didn't know how else to explain

gusty cargo
#

hey Zenvin, I found a way

#

instead of instantiating the projectiles in infinite loop, I could just instantiate a prefab that has a spawner script attached to it...
Something like:

float sec = 0f; 
void update()
{
  sec += Time.DeltaTime;
  if(sec > 0.2f)
  {
    // Instantiate and destroy commands
    sec = 0f;
  }
}```
earnest delta
earnest delta
gusty cargo
#

ok

#

thanks for your advise and sorry for not being able to understand it