Hey there my fellows, i have an issue with a C# code im trying to use in order to do some testing with a very simple health box, i am admittedly not strong in coding languages, and every time i try and fix the errors in the code, ill fix one but another comes up, making an endless loop of the same errors. it says i must compile the script into a folder that doesnt even exist on my computer. ive tried different ways of writing the script and it still doesnt work out. The one i would prefer to use reads as follows; using UnityEngine; using System.Collections; public class playerhealth : MonoBehaviour { public int maxhealth = 100; public int curhealth = 100; // Use this for initialization void Start () { } // Update is called once per frame void Update () { } void onGUI() { GUI.Box(new Rect(10, 10, Screen.width / (maxhealth / curhealth), 20), curhealth + "/" + maxhealth); } Any/All help will be greatly appreciated ^_^
Ah, good catch. That won't throw an error, it just won't draw your GUI. I happen to know this from experience, the cruelest teacher.
wow i cant believe that the only reason it wasn't working was because i forgot a closing brace on the end and i didn't capitalize an O... its amazing how such little things can ruin a script , thank you guys so much for the quick and accurate evaluation and corrections ^_^ I'm so happy that i finally have it working completely. I'm 100% certain ill have many more questions to come, and if everyone is as knowledgeable and fast as you guys i will be forever indebted to you all. *** now that i added the script the first person controller prefab is freaking out, im no longer pivoting from the mouselook but rather completely rotating the whole character/camera on multiple axes. NEVER MIND i fixed it myself! sorry >.>
Calm down. Code (csharp): Take a look at the code tag for putting code blocks into the forum. It uses a fixed-width font, making it a little easier to read.
heh sorry about the edits, i was really concerned at first but then i easily fixed the problem -^_^- another question though, im not sure i fully understand what it means when the error log says nullrefrenceexception im trying to run a script to show a debug line that is drawn to show what someone/something has targeted, but the line doesnt show up even though the object is still targeting me (in this case) correctly, basically im trying to use the line as a quick way to see straight line pathing.
A null reference exception is when you ask the computer to do something, but nothing is actually there where it expects data. It's caused by dereferencing a null reference. If you are using Debug.DrawLine(), it's probable that one of the Vector3 arguments it expects references something that is still null. Feel free to post your code (in the code tag).
Ok so heres the code, i believe the error is coming from the myTransform = transform line, but im not sure how to fix it. Code (csharp): using UnityEngine; using System.Collections; public class EnemyAI : MonoBehaviour { public Transform target; public int movespeed; public int rotationspeed; private Transform myTransform; void awake (){ myTransform = transform; } // Use this for initialization void Start () { GameObject go = GameObject.FindGameObjectWithTag("Player"); target = go.transform; } // Update is called once per frame void Update () { Debug. DrawLine(target.transform.position, myTransform.position, Color.red); } }
When you set target to go.transform it's possible that you are actually not getting something back from the FindGameObjectWithTag(). Have you verified in the editor that target actually has a value? Also, there appears to be a space between Debug. and DrawLine although that would not give you a null reference and might just be a mistake in the copy-paste.
Im not sure how to check if it has a value >.>, I have the tag on the object set to player, so it is targeting the player, but since im using the first person prefab, could it be that since it it trying to target the mesh and obtain its information, the mesh isnt being read as an object properly?
If you only have one enemy, which I hope is the case for now, please put this in there. Code (csharp): void OnGUI() { GUI.Label(new Rect(0, 0, 100, 50), target.name)); } After that adding that to your script at the end, on the top left of your screen, you should see the name of whatever it has for a target. You can move it around if you like by adjusting the rect.
When i put it in it shows the name of the literal object it is targeting, in this case it is named Graphics, since its from the prefab, and that is the object with the player tag, just in case you need to know ^_^
no i still have the nullrefrence error but it is targeting "me", which is odd since it requires the information from the targeting to draw the line, so the information is there, just its not being picked up, at least that's what i think.
Have you enabled Gizmos on the top right of the viewport in Game mode? It won't render without that, but with the Scene mode it will. I think we vastly overcomplicated it, sometimes things are hard to do over a forum.
I popped it quickly into one of my test projects. One of your problems is in EnemyAI where you use "void awake()" rather than "void Awake()". That was giving at least one null reference error. I had another issue that might have been related to me copying and pasting playerhealth.cs from your original post. I really don't know much about character encoding! Either way, straight up cutting it added an extra character between "10, " and "(Screen.width" that was causing an issue. I couldn't see it in Visual Studio, but it was there in the default Unity editor.
if(go != null) { target = go.transform; } Does it stop the errors? Another thing you can do is to have a public variable called target and drag the game object over to it in the inspector.
Ah i fixed it, all i had to do was capitalize Awake and click gizmos in the game screen. I feel so silly that all my issues have been such little things, but we came up with so many complicated possibilities hah, i guess i should be more careful with my capitalization >.>.