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().
– robertbuSorry. 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 :)
– xucian