Another way to reference scripts?

Hi, I am trying to find another way to reference a script so that another script can use its info(variables, etc)

I have been using a way that has been successful, but produces annoying error.

Here is the code

var other = gameObject.GetComponent(PlayerMovement);

like I said it works, but produces an error such as this

“UnityException: You are not allowed to call this function when declaring a variable.
Move it to the line after without a variable declaration.
If you are using C# don’t use this function in the constructor or field initializers, Instead move initialization to the Awake or Start function.
UnityEngine.Component.get_gameObject () (at C:/BuildAgent/work/cac08d8a5e25d4cb/Runtime/ExportGenerated/Editor/UnityEngineComponent.cs:183)
NoMovement…ctor () (at Assets/Scripts/NoMovement.js:5)”

Normally this wouldnt bother me much, but these errors are starting to build up

Any help would be greatly appreciated! Let me know if you need additional info!

I'm confused. Why don't you just do what the error indicates you should do: var other : PlayerMovement; other = gameObject.GetComponent(PlayerMovement); Typically I place the second line in Start().

Sorry. Didn't know I can't post this as an answer in multiple places. I do, however, think this will help folks searching for an asset like this. And this is indeed a valid answer. Anyone could've mentioned this asset in a comment (or even I from a different account). The simple fact that I'm posting this makes it an advertisement and I know it's not difficult to understand why... Sorry again. You can delete any other answers you think it's needed, if that's the rule :)

2 Answers

2

I assume you are setting your script up as such:

#pragma strict
var other = gameObject.GetComponent(PlayerMovement);

function Update()
{

}

Yes? Set it to this instead and it will work:
#pragma strict
var other : PlayerMovement;

    function Start()
    {
     other = gameObject.GetComponent(PlayerMovement)
    }
  
    function Update()
    {
    
    }

thanks for the quick response! I actually found out how to! var other : ScriptName; this works perfectly! thanks again! EDIT: thanks robertbu

Its cool. Where-as advertising is not really allowed on the site, us mods tend to agree that a couple of posts is ok. Its a fine line though between useful answer and spam :P When giving an asset as an answer it makes sense if a person is recommending someone else's Asset rather than promoting ones own. But it is a Grey Area. If you want to find the latest and most relevant Question to post on, I'm sure one more will be fine. Also, there is this section of the Forum which is geared towards promoting Asset Store products. http://forum.unity3d.com/forums/assets-and-asset-store.32/

You can’t dynamically assign member variables in the class definition; you have to do this another way. There are two common approaches.

  1. Drag the script that you want to reference to the inspector. This is the easiest.

  2. Do the GetComponent call in Awake or Start.

    var other : PlayerMovement;

    function Awake()
    {
    other = GetComponent(PlayerMovement);
    }