Really weird scene problem...

When I test a scene in my game, it works perfectly. But when I access the scene from another scene, or play it in the build of the game, some weird things happen(objects ). But I have had times when the scene works fine (when it is accessed from other scenes). I know this is a weird problem, does anyone have any ideas as to why this would happen?

Could you explain what’s happening?

There are two problems that I have noticed

1: A function is be repeatedly invoked (this is only supposed to happen when a random number equals a certain value).
2: Another object will not activate when a condition is met (the condition is when a variable equals a certain number).

You’re going to have to be more specific than that. Can you post some of your code and show any errors or warnings that are appearing?

Sure. Well first, I am getting no errors whatsoever.

This is the script I was referring to in my first problem. The “FIRE” function is only supposed to be invoked when the variable EHP = 2. But, when I get to the scene from another scene, it is being invoked when EHP = 1.

var EHP = 1;
var BEH	= 0;
var SPD	= 0;

var SHP1 		  :GameObject  ;
var SHP2 		  :GameObject  ;
var SHP3 		  :GameObject  ;
var SHP3_5 		  :GameObject  ;
var SHP4 		  :GameObject  ;
var SHP4_5 		  :GameObject  ;
var SHP5 		  :GameObject  ;
var Projectile 	  :GameObject  ;

var EXPLODE :GameObject;	

function OnTriggerEnter (other : Collider){
	if (other.gameObject.CompareTag ("Projectile")) {audio.Play();EHP-=1;Destroy (other.gameObject,0);}
}

function Start (){
SHP1.active=false;SHP2.active=false;
SHP3.active=false;SHP4.active=false;
SHP5.active=false;
SHP3_5.active=false;SHP4_5.active=false;
var E:int=Random.Range(1,(DEF_Player.LVL+1));
if(E==1){BEH=1;}if(E==2){BEH=2;}if(E==3){BEH=3;}
if(E==4){BEH=4;}if(E==5){BEH=5;}Invoke("ST",0);}

function Update(){
	transform.Translate(Vector3.forward *(-1*SPD) * Time.deltaTime);
	if(EHP==0){DEF_Player.SCR+=100*BEH;Destroy (gameObject,0);DEF_Timer.countDownSeconds+=2;
	Instantiate(EXPLODE, transform.position, transform.rotation);}}

function ST() {
	if(BEH==1){SPD=3;SHP1.active=true;}
	if(BEH==2){SPD=5;SHP2.active=true;InvokeRepeating("FIRE",0,4);}
	if(BEH==3){SPD=7;EHP=3;SHP3.active=true;SHP3_5.active=true;}
	if(BEH==4){SPD=9;SHP4.active=true;SHP4_5.active=true;}
	if(BEH==5){SPD=5;Invoke("SCALE",0);EHP=3;SHP5.active=true;}}

function FIRE (){Instantiate(Projectile ,transform.position,transform.rotation);}
function SCALE(){transform.localScale += Vector3(3,3,3);}

This is the second problem’s script. It is supposed to active the complete gameobject when LIF and LIFE equal 0 and 1 respectively. But, when they do, nothing happens.

static var SCR = 0;
static var LVL = 1;
static var LIF = 3;
static var BOM = 2;

var LIFE			   = 1;
static var POWERUP     = 0;

var projectile : GameObject;
var BOMB       : GameObject;
var SPAWN: 		 GameObject;

var PG1: 		 GameObject;
var PG2: 		 GameObject;
var PG3: 		 GameObject;

var complete : GameObject;

private var moveDirection : Vector3 = Vector3.zero  ;
function Start () {InvokeRepeating("SPAWNER",4,2);InvokeRepeating("powerup",20,20);InvokeRepeating("rapidfire",4,.1);}

	function Update (){
	//MOVEMENT
    	var controller : CharacterController = 
    	GetComponent(CharacterController);
    	if (controller.isGrounded) {
        moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,0);
        moveDirection = transform.TransformDirection(moveDirection);
        moveDirection *= 30;}moveDirection.y -= 30 * Time.deltaTime;
	    controller.Move(moveDirection * Time.deltaTime);
	//LIFE
   		if(LIFE==1){if (LIF==0){gameObject.SetActiveRecursively(false);complete.SetActiveRecursively(true);}}
 
 		
	//LEVEL.
		if((SCR>=1500)  (SCR<=3000)){LVL=2;}if((SCR>=3000)  (SCR<=6000)){LVL=3;}
		if((SCR>=12000)  (SCR<=30000)){LVL=4;}if((SCR>=30000)		       ){LVL=5;}
 

	//WEAPON
		if( Input.GetButtonDown( "Fire1" )){Instantiate(projectile,transform.position,transform.rotation);}
		if( Input.GetButtonDown( "Jump"  )){if(BOM>=1){Instantiate(BOMB,transform.position,transform.rotation);BOM-=1;}}}
		//POWERUP	
		    function rapidfire (){if(POWERUP>=0){Instantiate(projectile,transform.position,transform.rotation);POWERUP-=1;}}
			function powerup(){
				var E:int=Random.Range(1,4);
					if(E==1){Instantiate(PG1, new Vector3(Random.Range(36, 36), 1, 40), 	Quaternion.identity);}
					if(E==2){Instantiate(PG2, new Vector3(Random.Range(36, 36), 1, 40), 	Quaternion.identity);}
					if(E==3){Instantiate(PG3, new Vector3(Random.Range(36, 36), 1, 40), 	Quaternion.identity);}}


	//GUI
		function OnGUI () {
    		GUI.Label (Rect (875, 10, 100, 20), SCR.ToString());
    		GUI.Label (Rect (120, 10, 100, 20), BOM.ToString());
		    if(LIFE==1){GUI.Label (Rect (050, 10, 100, 20), LIF.ToString());}}
		   
	//COLLIDER
		function OnTriggerEnter (other : Collider){
			if (other.collider.name == "Enemy(Clone)"){LIF-=1;}if (other.collider.name == "Enemy_Projectile(Clone)"){LIF-=1;Destroy(other.gameObject);}}
  			
	//SPAWN
	    function SPAWNER (){Instantiate(SPAWN, new Vector3(Random.Range(-47, 36), 1, 40), 	Quaternion.identity);}

Anyone have any ideas as to why these things are happening?

Anyone? I know I sound desperate, but I honestly have no idea as to why this would be happening.

With everything on one line and no comments, it’s pretty hard to read.

I’d suggest putting some Debug.Log in, or making a couple of the troublesome variables public so you can see what’s going on.

This might be the most infuriating bug I have encountered in Unity.

I figured it out (kinda)! It is adding 2 to some of the conditions. If something is “if (variable==0){do something}” the “do something” is happening when the variable equals two. Now why this is happening when I access the scene from other scenes is anyones guess, but at least I know what the problem is.