0
votes

In my unity car game project, I have four car(one player car) with respective four indicators (one main Indicator). Indicator moves vertically upward on update function and each indicator move with random speed. In case of car, player car does not move upward and downward, it only moves left and right by the user. Similarly other car also does not move left,right,forward,backward. Rather the other car shows forward and backward move animation with the difference between the indicators(there are four indicator, indicators means three indicator that references three cars other than player car) and main indicator position(main indicator is reference for player car).

I have two scene, one for user interface( that contain play button,exit button etc.). And another scene for game play.

When I click on Play Game button then it goes to game scene and the racing game start with no error. After the completion of game, user interface scene loads automatically saying "Replay Game", "Exit Game". In this case if I click on "Replay Game" button, the game scene opens and indicator start to move but the car does not move( does not show animation) with respect to the movement of indicator. And the error occur here saying

Missing Reference Exception: The object of type moveCar(it is script attached to the car object) has been destroyed but you are still trying to access it.Your script should either check if it is null or you should not destroy the object.

During the error, I check the game object car and it contains movecar script but there is still same error. As the script is working fine on first step so I did not have attach code here. Any help would be greatly appreciated. I am getting stop in this problem for many days.

2
Please provide the complete stack trace of the error and the handler code of the Replay Game button. - zwcloud
Please add the code when you destroy and instanciate the new car - joreldraw
@zwcloud , there is now new code for replay game button. It just load the game scene as Play button does . As I have mentioned in question, when user click on play button, it loads game scene and the game runs fine with no error. But after the completion of this level, user interface scene is loaded with replay button". In this case when user click on **replay button, gaming scene is loaded and the indicator starts to move but the car do not move and above mentioned error is displayed. - ero

2 Answers

0
votes

Before loading your scene on the replay button add this line of code DontDestroyOnLoad(playerCarObjectHere);

0
votes

I have used static variable and event in the game. During the first play, the game runs fine but when I restart/replay the game, I got an error saying MissingReferenceException. As the static variable remains same as long as the program runs, even I restart/replay the game. So I make a function to assign null to the static variable during game load.

public class moveCar : MonoBehaviour
{
    public delegate void RCarIdle(); 
    public static event RCarIdle carIdle;   
    public delegate void StepForward();      
    public static event StepForward EstepForward;

    void Start()
    {
        ......
        ......
        makeNull();
    }   

    void makeNull()
    {  
        carIdle=null;    
        EstepForward=null;  
        //similarly other static variable are set to null or a default/initial value
    }
}

Finally, I am able to solve this error. So, we need to be very careful while using static variable as its value will be same throughout the program. In case of unity also, its(static variable) value does not get reset even if you reload the scene.