1
\$\begingroup\$

I have a game where:

  • player (green cube)
  • enemies (red cubes)
  • floor (blue plane)

Image before losing (player colliding with enemy):

enter image description here

Image after restarting game:

enter image description here

GameOver Code (Player.cs):

void Update () {
    if(!gameOver){
        move();
    }else{
        if(Input.GetKey("space")){
            SceneManager.LoadScene("Cena1");
        }
    }
}

void OnCollisionEnter(Collision obj){
    if(obj.gameObject.tag=="Inimigo"){
        canvas.gameObject.SetActive(true);
        gameOver=true;
    }       
}

The problem happens every time I collide the player with the enemy and press the spacebar to restart the scene.

The same thing happens if I use: SceneManager.LoadScene("Cena1");.


EDIT 1 (Added full scripts)

script_Player.cs:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class script_Player : MonoBehaviour {

    private float speed=0.4f;
    private float x,y,z,iniX,iniY,iniZ;
    private GameObject canvas;
    private bool gameOver=false;
    public Rigidbody rb;
    // Use this for initialization
    void Start () {
        canvas=GameObject.Find("Canvas");
        canvas.gameObject.SetActive(false);
        iniX=transform.position.x;
        iniY=transform.position.y;
        iniZ=transform.position.z;
    }

    // Update is called once per frame
    void Update () {

        if(!gameOver){
            move();
        }else{
            if(Input.GetKey("space")){
                SceneManager.LoadScene("Cena1");
            }
        }

        getPositions();

        parede();

    }

    void move(){
        if(Input.GetKey("w")){
            transform.position+=new Vector3(0,0,speed);
        }
        if(Input.GetKey("s") && transform.position.z>-10){
            transform.position+=new Vector3(0,0,-speed);
        }
        if(Input.GetKey("a") && transform.position.x>-4.25){
            transform.position+=new Vector3(-speed,0,0);
        }
        if(Input.GetKey("d") && transform.position.x<4.25){
            transform.position+=new Vector3(speed,0,0);
        }

        if(Input.GetKey("space")){
            //transform.position+=new Vector3(0,0.3f,0);
            rb.AddForce(0,100,0);
        }
    }

    void getPositions(){
        x=transform.position.x;
        y=transform.position.y;
        z=transform.position.z;
    }

    void parede(){
        if(transform.position.x<-4.25){
            transform.position=new Vector3(-4.25f,y,z);
        }
        if(transform.position.x>4.25){
            transform.position=new Vector3(4.25f,y,z);
        }
        if(transform.position.z<-10){
            transform.position=new Vector3(x,y,-10);
        }
    }

    void OnCollisionEnter(Collision obj){
        if(obj.gameObject.tag=="Inimigo"){
            //Destroy(obj.gameObject);
            //SceneManager.LoadScene("Cena1");
            canvas.gameObject.SetActive(true);
            gameOver=true;
        }       
    }
}

script_Inimigo.cs:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class script_Inimigo : MonoBehaviour {

    private bool left=false,right=false;
    public float speed=0.08f;
    private float x,y,z,y2;
    // Use this for initialization
    void Start () {
        z=transform.position.z;
        left=true;
    }

    // Update is called once per frame
    void Update () {

        GameObject player=GameObject.Find("Player");
        y2=player.transform.position.y;

        x=transform.position.x;
        transform.position=new Vector3(x,y2,z);

        if(left){
            transform.position+=new Vector3(-speed,0,0);
            if(transform.position.x<-4.25){
                left=false;
                right=true;
            }
        }else if(right){
            transform.position+=new Vector3(speed,0,0);
            if(transform.position.x>4.25){
                right=false;
                left=true;
            }
        }

        if(z+3<player.transform.position.z){
            Destroy(gameObject);
        }

    }
}
\$\endgroup\$
  • 4
    \$\begingroup\$ Can you reproduce the problem in a new empty project that includes only these scripts shown here? If not, then there's a good chance there's a relevant detail you haven't recorded for us to work with. \$\endgroup\$ – DMGregory Aug 13 at 1:34
  • \$\begingroup\$ @DMGregory I created a new project and this time when I die, only the floor material is removed. I added the full scripts to the question. \$\endgroup\$ – Boneco Sinforoso Aug 13 at 14:28
  • 1
    \$\begingroup\$ @BonecoSinforoso Have you verified that the materials are actually changing in the inspector window? I suspect this is actually a problem with automatically-generated lighting. As a quick test to see if this is the case, un-tick "Auto-Generate" in the lighting window and hit "Generate Lighting" automatically. \$\endgroup\$ – Chaosed0 Aug 13 at 18:08
  • 1
    \$\begingroup\$ This looks like a lighting issue. Are you using static/realtime light baking? If yes, try disabling both of them and bake. That should clear all old lighting data. \$\endgroup\$ – Chillersanim Aug 13 at 20:44
  • \$\begingroup\$ @Chaosed0 It worked with me. \$\endgroup\$ – Boneco Sinforoso Aug 14 at 0:31
0
\$\begingroup\$

Whenever you have a strange change in your scene, remember to verify your lighting settings. In this case, the problem is caused by the "Auto-generate Lighting" setting within the Unity lighting window being checked. If you manually generate the lighting by un-checking this option and hitting "Generate", then the problem should go away.

Note that lighting changing with in the editor when a scene changes is a persistent and common issue.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.