Javascript reference other object

Hey all,

I am very new to unity and coding, i have hacked together a simple raycasting script that i attached to the FPS controller and it spits out an objects name to the debug console when i click it

Now i have created a new object with a guitext on the ui layer that i want to display the object name there

my added code in function update is:

var debugtxt = gameObject.GetComponent(“DebugText.GUIText”);
debugtxt.text = (“TEXT”);

It instead gives me this error

NullReferenceException: Object reference not set to an instance of an object
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[ ] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[ ] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.SetProperty (System.Object target, System.String name, System.Object value)
raycasttest.Update () (at Assets/raycasttest.js:17)

So i can see i am not referencing the DebugText GUItext component properly, how can i do this?

You need to get the component only; “DebugText.GUIText” isn’t valid syntax. See the docs for GetComponent for examples, also here. Also never use strings in GetComponent.

–Eric