Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

C# newbie, with a question

Discussion in 'Scripting' started by Quadblackhawk, Jun 3, 2011.

  1. Quadblackhawk

    Quadblackhawk

    Joined:
    Jun 3, 2011
    Posts:
    9
    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 ^_^
     
  2. JRavey

    JRavey

    Joined:
    May 12, 2009
    Posts:
    2,377
    Other than having no closing brace at the end, it looks fine to me. What is the actual error?
     
  3. GisleAune

    GisleAune

    Joined:
    May 16, 2011
    Posts:
    88
    It should be void OnGUI(), not onGUI.

    Can you post what errors the console throw at you?
     
  4. JRavey

    JRavey

    Joined:
    May 12, 2009
    Posts:
    2,377
    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.
     
  5. Quadblackhawk

    Quadblackhawk

    Joined:
    Jun 3, 2011
    Posts:
    9
    :D 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 o_O, 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 >.>
     
    Last edited: Jun 3, 2011
  6. JRavey

    JRavey

    Joined:
    May 12, 2009
    Posts:
    2,377
    Calm down. :)

    Code (csharp):
    1.  
    2. Take a look at the code tag for putting code blocks into the forum.
    3. It uses a fixed-width font, making it a little easier to read.
    4.  
     
  7. Quadblackhawk

    Quadblackhawk

    Joined:
    Jun 3, 2011
    Posts:
    9
    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.
     
  8. JRavey

    JRavey

    Joined:
    May 12, 2009
    Posts:
    2,377
    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).
     
  9. Quadblackhawk

    Quadblackhawk

    Joined:
    Jun 3, 2011
    Posts:
    9
    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):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EnemyAI : MonoBehaviour {
    5.    
    6.     public Transform target;
    7.     public int movespeed;
    8.     public int rotationspeed;
    9.    
    10.     private Transform myTransform;
    11.    
    12.     void awake (){
    13.         myTransform = transform;
    14.     }
    15.     // Use this for initialization
    16.     void Start () {
    17.         GameObject go = GameObject.FindGameObjectWithTag("Player");
    18.        
    19.         target = go.transform;
    20.    
    21.     }
    22.    
    23.     // Update is called once per frame
    24.     void Update () {
    25.         Debug. DrawLine(target.transform.position, myTransform.position, Color.red);
    26.    
    27.     }
    28. }
    29.  
     
  10. JRavey

    JRavey

    Joined:
    May 12, 2009
    Posts:
    2,377
    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.
     
  11. Quadblackhawk

    Quadblackhawk

    Joined:
    Jun 3, 2011
    Posts:
    9
    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?
     
  12. JRavey

    JRavey

    Joined:
    May 12, 2009
    Posts:
    2,377
    If you only have one enemy, which I hope is the case for now, please put this in there.

    Code (csharp):
    1.  
    2. void OnGUI()
    3. {
    4.     GUI.Label(new Rect(0, 0, 100, 50), target.name));
    5. }
    6.  
    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.
     
  13. Quadblackhawk

    Quadblackhawk

    Joined:
    Jun 3, 2011
    Posts:
    9
    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 ^_^
     
  14. JRavey

    JRavey

    Joined:
    May 12, 2009
    Posts:
    2,377
    Hmm...that's good to know.

    But it's not drawing? It's also not giving you the null reference error?
     
  15. Quadblackhawk

    Quadblackhawk

    Joined:
    Jun 3, 2011
    Posts:
    9
    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.
     
  16. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
    replace myTransform with transform.
     
  17. JRavey

    JRavey

    Joined:
    May 12, 2009
    Posts:
    2,377
    I've never played with Debug.DrawLine, so I am going to play with it for a minute.
     
  18. JRavey

    JRavey

    Joined:
    May 12, 2009
    Posts:
    2,377
    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.
     
  19. Deccato

    Deccato

    Joined:
    Jun 4, 2011
    Posts:
    2
    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.
     
  20. GisleAune

    GisleAune

    Joined:
    May 16, 2011
    Posts:
    88
    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.
     
  21. JRavey

    JRavey

    Joined:
    May 12, 2009
    Posts:
    2,377
    Deccato, I think that's it.
     
  22. Quadblackhawk

    Quadblackhawk

    Joined:
    Jun 3, 2011
    Posts:
    9
    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 >.>.
     
  23. JRavey

    JRavey

    Joined:
    May 12, 2009
    Posts:
    2,377
    We should all be more careful!