0

I am new on unity and I am dealing with a problem. Unity doesn't run my c# scripts in any component and in any project. Although unity reads all the values from the script, it won't execute the game as expected. On the script components it shows that the scripts are disabled, as you can see on the following screenshot. I should probably mention that the game was working fine until this problem came from nowhere. I don't think that it's a problem from the script, because this happens even on completely empty scripts. This is the script component Empty c# script with the same problem. I have tried to reinstall unity 2 times(nothing happens). I have tried to reimport all the scripts(nothing happens). I have tried to reimport all the assets(nothing happens). Any help would be really appreciated. This is the whole scene

This is the whole script:

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

[System.Serializable]
public class Boundary
{
    public float xMin, xMax, zMin, zMax;
}    

public class PlayerController : MonoBehaviour
{
    public float speed;
    public float tilt;
    public Boundary boundary;
    private Rigidbody rb;
    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    public GameObject shot;
    public Transform shotSpawn;
    public float fireRate;
    private float nextFire;
    void Update()
    {
        if (Input.GetButton("Fire1") && Time.time > nextFire)
        {
            nextFire = Time.time + fireRate;
            Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
            GetComponent<AudioSource>().Play();
        }
    }

    private void FixedUpdate()
    {
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");

        Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
        rb.velocity = movement * speed;

        rb.position = new Vector3
            (
            Mathf.Clamp(rb.position.x, boundary.xMin, boundary.xMax),
            0.0f,
            Mathf.Clamp(rb.position.z, boundary.zMin, boundary.zMax)
            );
        rb.rotation = Quaternion.Euler(0.0f, 0.0f, rb.velocity.x * -tilt);
    }


}

This is the screenshot

3 Answers3

0

First off - the script has the embedded assumption that the Input Manager (Project settings / Input) has an axis called "Fire1". While this is true by default, I've wittnesed it change (usually by means of a 3rd party script) so please verify that you have that axis in your project. You can try creating a new project and placing your scripts there.

Secondly, I am not sure if you are doing the correct thing with your rigidbody, you seem to be manually changing its position, so you are effectively overriding any computations that you ask for earlier (by setting the velocity), you should pick one approach and stick with it, i.e. either tick 'isKinematic' on the rigidbody and control its position by hand, or just trust PhysiX and modify velocity (makes more sense to me). Mixed approach can give some unexpected results in a lot of cases, where it would sort of work but not quite as expected

zambari
  • 4,797
  • 1
  • 12
  • 22
0

I'm not sure exactly what is not working in your code, but if the problem is your FixedUpdateMethod, then it is because you changed the access level of the method.

This is a link explaining the why you can't change access levels when overriding a method.

Unity's API has FixedUpdate() at default access level so try changing taking private off of it.

Should look something like this:

public class PlayerController : MonoBehaviour
{
    public float speed;
    public float tilt;
    public Boundary boundary;
    private Rigidbody rb;
    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    public GameObject shot;
    public Transform shotSpawn;
    public float fireRate;
    private float nextFire;
    void Update()
    {
        if (Input.GetButton("Fire1") && Time.time > nextFire)
        {
            nextFire = Time.time + fireRate;
            Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
            GetComponent<AudioSource>().Play();
        }
    }

   void FixedUpdate() //no longer has private keyword appended to method
    {
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");

        Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
        rb.velocity = movement * speed;

        rb.position = new Vector3
            (
            Mathf.Clamp(rb.position.x, boundary.xMin, boundary.xMax),
            0.0f,
            Mathf.Clamp(rb.position.z, boundary.zMin, boundary.zMax)
            );
        rb.rotation = Quaternion.Euler(0.0f, 0.0f, rb.velocity.x * -tilt);
    }


}
Thatalent
  • 404
  • 2
  • 8
0

Had the same issue, took a while to find the solution you may have pause on errors on in your unity debug console, it pause the execution of the scripts if there is any errors, There is a tab in here labelled 'Error Pause'. If it is highlighted, click it to disable it

TLG
  • 1