How to make a game in Unity, game manager error coding script

Updated on September 2, 2017 in [D] Game Dev. gossip
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
22 on August 27, 2017

Hi, i am a beginner in unity and in C# I have an issue with colliding with objects. Whenever i hit an obstacle/object my game is programmed to restart but it doesn’t when i hit the obstacle the game keeps on going and doesn’t restart but however i also programmed to when it falls out the void it restarts and it does work but colliding with obstacles doesn’t work and there is no coding error that the game can detect so i don’t know whats wrong with my scripts. Can anyone help me please. Here is the coding scripts that are related to the issue (there are 3 of them altogether)

 

 

    1. player movement script:

[
using UnityEngine;

public class playerprogrammingmovemnt : MonoBehaviour
{

    //This is a reference to the rigidbody component called “rb”
    public Rigidbody rb;

    public float forwardForce = 2000f;
    public float sidewaysForce = 500f;

    // We marked this as “fixed”update becuase we are using to mess with physics.
    void FixedUpdate()
    {
        rb.AddForce(00, forwardForce * Time.deltaTime);   //Add a forward force

        if (Input.GetKey(d))
        {
            rb.AddForce(sidewaysForce * Time.deltaTime, 00, ForceMode.VelocityChange);
        }

        if (Input.GetKey(a))
        {
            rb.AddForce(-sidewaysForce * Time.deltaTime, 00, ForceMode.VelocityChange);
        }

        if (rb.position.y < –1f)
        {
            FindObjectOfType().EndGame();
        }
    }
}
]

[/code]

 

2. Player collision script:

 

[ using UnityEngine;

public class playercollision : MonoBehaviour
{

    public playerprogrammingmovemnt movement;

    void onCollisionEnter(Collision collisionInfo)
    {
        if (collisionInfo.collider.tag == obstacle)
        {
            movement.enabled = false;
            FindObjectOfType().EndGame();
        }

    }

}
]

[/code]

 

3. Game Manager script:

 

[ using UnityEngine;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour {

    bool gameHasEnded = false;

    public float restartDelay = 1f;

    public void EndGame()

    {
        if (gameHasEnded == false)
        {
            gameHasEnded = true;
            Debug.Log(YOU FAILED);
            Invoke(Restart, restartDelay);
        }
       
    }

   void Restart ()  
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }

}
]

[/code]

 

P.S: there is no error’s the game works perfectly. it’s just that whenever i hit an obstacle/object the game is supposed to restart but it doesn’t the only time it restarts is when i fall of the void/out of the world.

  • Liked by
Reply
3 on August 27, 2017

I think you need to learn the basics of C# before following game making tutorials because you will face a lot of problems that are easier to solve if you can debug your program. This is not a hate message to you but it will help you on your way when developing games or systems in C# with Unity.

on August 27, 2017

ok, thanks for your advise. But i really wanted to know how you fix this. But thank you very much for responding to my question.

Guru
on August 28, 2017

We can’t tell you how to fix it if we don’t know anything like what errors you’re getting or not.

on August 28, 2017

I am getting this error:

 

Assets/GameManager.cs(8,5): error CS1002: ; expected

Show more replies
  • Liked by
Reply
Cancel
5 on August 28, 2017

add a ; after Debug.Log(“GAME OVER”)


like this

using UnityEngine;


public class GameManager : MonoBehaviour {


     void EndGame()
     {

           Debug.Log(“GAME OVER”)
;
     }
 

}

and i suggest to learn the basics first like bennie said.

on August 28, 2017

thanks,  can you help me with this piece of coding as well:

 


using UnityEngine;

public class playercollision : MonoBehaviour {



 public playerprogrammingmovemnt movement;


public GameManager gameManager;



 

void onCollisionEnter(Collision collisionInfo)

 {


       if (collisionInfo.collider.tag == “obstacle”)

                {


           movement.enabled = false;


           FindObjectOfType<GameManager>().EndGame();


               }



    }



 

}



 

Guru
on August 29, 2017

We can’t tell you how to fix it if we don’t know anything like what errors you’re getting or not.

From Job

on August 29, 2017

I am getting this error:

Assets/scripts/playercollision.cs(13,45): error CS0122: `GameManager.EndGame()’ is inaccessible due to its protection level

 

Guru
on August 29, 2017

Make sure the EndGame method is public

on August 29, 2017

i don’t understand.

Show more replies
  • Liked by
Reply
Cancel
11 on August 29, 2017

1. Go to the GameManager script

2. Find the EndGame method. It should look something like this:

void EndGame()
{
//Some code here
}

3. Add the public access identifier to the method:

public void EndGame()
{
//Some code here
}

on August 29, 2017

Ok thanks!!!!!! 😀😁

on August 29, 2017

There is one more thing you could help me with tough, on brackey’s youtube channel (the series: how to make a game in unity) i have been following the tutorial and i have been having problems with his ‘game over’ video. When i hit an obstacle the level doesn’t restart but if i fall out of the void it does but when i hit an obstacle it doesn’t restart so could anyone help me with that please. thank you!

 

Guru
on August 29, 2017

Please post a new question for a new problem and again, we can’t do anything without code/error messages.

on August 30, 2017

Ok, but there is no error this time.

on August 30, 2017

we need scripts man, BUT NOT ALL OF THEM, JUST THE PARTS THAT MATTER.

Also try rewatching the brackeys tutorial where he does the game over and check if everything in your scene and code is alright before posting here 🙂

on August 31, 2017

ok, but i can’t really show you my coding scripts because whenever i take a screenshot and try to send it to you guys it says ‘admin disabled this function‘. How do you send coding script pictures?

Guru
on August 31, 2017

Don’t use pictures just copy paste and use the insert codes function

on August 31, 2017

what insert code function?

on September 2, 2017

How do you use the insert code function?

Guru
on September 2, 2017

When you’re typing your comment the right most button.

Show more replies
  • Liked by
Reply
Cancel