Search Unity

What could this error mean?

Discussion in 'Scripting' started by dogzerx2, Aug 31, 2011.

  1. dogzerx2

    dogzerx2

    Joined:
    Dec 27, 2009
    Posts:
    3,967
    I'm making this soldier script for the asset store, but someone is getting a specific error and I don't know what could be causing it!

    My guess this is about how I pass the variables from one script to the other.
    I'm passing variables from one script to the other using GetComponent()

    For example, fallSpeed is a global variable in soldier.js, and I get the variable like this

    Code (csharp):
    1. private var soldierScript : MonoBehaviour;
    2.  
    3. function Start(){
    4.    soldierScript = soldier.GetComponent("soldier");
    5. }
    6.  
    7. function Update{
    8.     var fallSpeed : float = soldierScript.fallSpeed;
    9. }
    Everything works in my computer, but in his computer he's getting this error:

    Code (csharp):
    1. SoldierCamera.js; fallspeed is not a member of unity.monobehaviour
    2.  
    What do you guys think might be causing that, and how do I solve it?
     
  2. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    959
    It's because the type of soldierScript is set to MonoBehaviour in its declaration. Set it to Soldier (or whatever the script is called) instead.
     
  3. Alienchild

    Alienchild

    Joined:
    Oct 14, 2010
    Posts:
    364
    A script is an object in Unity, meaning that if you create a script called Soldier.js then you can create variables of that object type, like so:

    Code (csharp):
    1. var myScriptVariable : Soldier;
    When you then fetch the script from another component you do not need to add the quotes, because what you are getting is the entire script (in this case Soldier) object, with all its variables and whatnot. So to get the reference to the Soldier.js script on a gameobject you do as follows:

    Code (csharp):
    1.  myScript = GameObjectReference.GetComponent(Soldier);
    In this example I already have a reference to the object that contains the script (GameObjectReference). Now let's say that you have a variable named speed, then you can simply modify/access it as follows:

    Code (csharp):
    1. myScript.speed = 10;
    or

    Code (csharp):
    1. if(myScript.speed > 10)
    Remember that the variable speed need to be set explicitly as public if it resides in a C# script. In javascript it is the other way around, where it is public (accessible from other scripts) as long as it is not set to private.

    Hope it helps, and as someone who have bought your soldier I must say it is a really fine asset. Hope you'll add more variations to it, such as different armors and weapons. Wouldn't mind paying more if you made it an add-on ;)
     
  4. dogzerx2

    dogzerx2

    Joined:
    Dec 27, 2009
    Posts:
    3,967
    I'm glad I know this now! Thanks!!