Search Unity

NullReferenceException

Discussion in 'Scripting' started by AlbertoT, Apr 13, 2009.

  1. AlbertoT

    AlbertoT

    Joined:
    Mar 27, 2009
    Posts:
    159
    Hello

    I have 1 GameObject : player with 2 components attached : UpdatePlayer and GetTarget

    In GetTarget I calculate the angle (ang) between player and enemy
    Using a cross product I see if the enemy is on the right or on the left side(dir[1])

    In UpdatePlayer I call ang and dir[1] but I get the error msg :

    NullReferenceException: Object reference not set to an istance of an object

    However everything is working fine

    I quote only up to the line which generates the error msg

    function Update () {

    var Target = GetComponent(GetTarget);

    // ruota player
    var angle = Target.ang;
    var k = Target.dir[1];

    var rot = k*angle ; // here I get the error msg


    ................

    }

    What am I doing wrong ?

    Thanks in advance
     
  2. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    That's hard to say. On one hand you claim everything is working ok (what makes you say that?) and on the other you cite an error (NullReferenceException), which is it? Have you done anything to verify that Target is a valid reference to your GetTarget component?
     
  3. AlbertoT

    AlbertoT

    Joined:
    Mar 27, 2009
    Posts:
    159
    Thanks

    I want to make an RPG movement

    My player must rotate and translate , following the mouse pointer regardless of the camera position and orientation

    Well, it works :D

    I can drag my character around on the terrain

    If I print(Target.ang) I get the right angle
    If I print(Target) I get GetTarget
    If I dont print anything I get that bloody msg :(

    P.S.

    To make it simple I said the angle between the player and the enemy but I meant the mouse pointer
     
  4. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    If it's not too lengthy can you post your full script? If the reference being shown is valid and "working" then it's not the source of the error. The fact that the line number being referred to seems "off" I'm suspecting something else in your code is causing these issues.
     
  5. AlbertoT

    AlbertoT

    Joined:
    Mar 27, 2009
    Posts:
    159
    No problem, it is quite simple
    I am making some test , I purchased Unity just a couple of weeks ago

    The componets attached ro the player are :

    GetTarget

    var targetDir : Vector3;
    var hit : RaycastHit ;

    var ang;
    var dir : Vector3;
    var dist;

    function Update () {

    var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    Physics.Raycast (Camera.main.transform.position,ray.direction,hit,1000);

    targetDir.x = hit.point.x - transform.position.x;
    targetDir.y = 0;
    targetDir.z = hit.point.z - transform.position.z;

    ang = Vector3.Angle(targetDir, transform.forward);
    dir = Vector3.Cross(targetDir, transform.forward);
    dir.Normalize();

    }

    and UpdatePlayer

    function Update () {

    var Target = GetComponent(GetTarget);

    // ruota player
    var angle = Target.ang;
    var k = Target.dir[1];

    var rot = -100*k*angle / 180;
    transform.Rotate(Vector3.up *rot*Time.deltaTime);

    transform.Translate(0,0, 10*Time.deltaTime);

    }

    Then I have an UpdateCamera attached to the camera
     
  6. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    Don't you usually have to put scriptname types in quotes?
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    No, that slows things down. It's faster to specify the type directly instead of parsing strings. The only time you'd want to use strings is if you're doing something like trying to access a C# script from Javascript and because of script compilation order issues, you can't directly specify the type.

    --Eric
     
  8. tgraupmann

    tgraupmann

    Joined:
    Sep 14, 2007
    Posts:
    828
    GetComponent takes a string or a type.
    http://unity3d.com/support/documentation/ScriptReference/Component.GetComponent.html

    The other GetComponent methods take just a type. A minor inconsistency.

    Code (csharp):
    1. var Target = GetComponent(GetTarget);
    2.  
    3. if (null != Target)
    4. {
    5.  
    6. // ruota player
    7. var angle = Target.ang;
    8. var k = Target.dir[1];
    9.  
    10. var rot = -100*k*angle / 180;
    11. transform.Rotate(Vector3.up *rot*Time.deltaTime);
    12.  
    13. transform.Translate(0,0, 10*Time.deltaTime);
    14. }
    Don't call GetComponent in every update frame.