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
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?
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 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
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.
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
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
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): var Target = GetComponent(GetTarget); if (null != Target) { // 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); } Don't call GetComponent in every update frame.