C# Script not working as expected
Hi,
I have been working with unity for a while, but have recently thought about making a game. So, as you can guess, I tried to make one. It was working well for a while. However, as soon as I started expanding on one of my scripts by adding health and stamina, it stopped noticing that the script was there. Unity was picking up that the script was there and I was changing it, but for some reason it wasn't applying anything I did, to the point where a normal print() statement in the Awake() function wouldn't do anything. I can remove the parts that make my player move, and the player will stop moving as expected, but trying to add a run feature, a health bar, stamina bar, or even print() no longer works.
The whole script is very large, but you can mimic the issue I'm having by trying the following script:
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class Player_Controller : MonoBehaviour {
public float walkingSpeed = 0.15f;
public float runningSpeed = 0.15f;
public float jump_height = 10.0f;
public float strongGravity = -30.0f;
public float weakGravity = -9.81f;
public bool lockJump_Active = false;
public bool disableDoubleJump = false;
public bool ignoreStamina = false;
private bool Can_Jump = false;
private float Translation;
private float Straff;
private float stamina = 100;
private float health = 100;
private CharacterController controller;
public Rigidbody rb;
public Slider healthSlider;
public Slider staminaSlider;
void Awake () {
//First testing to see if script is running
print ("Update");
Debug.Log ("update");
Cursor.lockState = CursorLockMode.Locked;
controller = GetComponent<CharacterController> ();
}
}
I know it is a lot for just 1 script (and there is much MUCH more that I have excluded), but I can't figure out what I have done wrong. I have tried removing component then adding it again, I have tried restarting Unity, but nothing seems to work.
I am using Unity 2017.1.0f3 on a windows 7 64 bit system.
Thanking all of you in advance.
Answer by Technokid2000 · Jan 01, 2018 at 06:40 AM
Not entirely sure what I did, but the script is now working quite nicely, apart from print() and Debug.Log() functions which for some reason still aren't working. I realised that I never set the min/max values for the sliders (which I have now fixed), and also that running speed should have been set to 0.25f, not 0.15f, which was the same as walking speed. So most of my issues have been fixed, but does anyone know why print() still isn't working?
My new (almost) working script is below:
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class Player_Controller : MonoBehaviour {
public float walkingSpeed = 0.15f;
public float runningSpeed = 0.25f;
public float jump_height = 10.0f;
public float strongGravity = -30.0f;
public float weakGravity = -9.81f;
public bool lockJump_Active = false;
public bool disableDoubleJump = false;
public bool ignoreStamina = false;
private bool Can_Jump = false;
private float Translation;
private float Straff;
private float stamina = 100;
private float health = 100;
private CharacterController controller;
public Rigidbody rb;
public Slider healthSlider;
public Slider staminaSlider;
void Awake () {
//First testing to see if script is running
print ("Starting Script - print");
Debug.Log ("Starting Script - debug.log");
healthSlider.maxValue = 100;
staminaSlider.maxValue = 100;
healthSlider.minValue = 0;
staminaSlider.minValue = 0;
Cursor.lockState = CursorLockMode.Locked;
controller = GetComponent<CharacterController> ();
}
}
Am I missing something obvious with the print() and Debug.Log() functions?
Again, thanking you all in advance.
In the end, I found the issue. I had turned off info and warnings from the console without realising. The only thing still active was errors, which I found by doing:
Debug.LogError("error");
Then, I turned on info and warnings. To anyone having the same issue, try that. A link to a post that explains it better is here: https://answers.unity.com/questions/530784/console-doesnt-work.html
Answer by brenttonkgames · Jul 14, 2020 at 04:14 PM
c# is not working i use visual studio c# for unity i was following along with a tutorial i did the code right and i had nothing but errors somebody please help me
Please put this as a seperate thread. For people to be able to help you, please post the code you are attempting to use, as well as the actual errors you are having. This is like saying "$$anonymous$$y car won't work, help!" without actually saying what part of the car isn't working, the symptoms, what you've tried, what car you actually have, etc. Please don't necro an old thread, because you won't get a problem solved that way. Seperate new problem, seperate new thread