Search Unity

Javascript reference other object

Discussion in 'Scripting' started by serpentxx, Jul 30, 2014.

  1. serpentxx

    serpentxx

    Joined:
    Jul 26, 2014
    Posts:
    2
    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?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    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