Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Stuck with GetComponent

Discussion in 'Scripting' started by Scellow, Jun 2, 2013.

  1. Scellow

    Scellow

    Joined:
    Jan 25, 2013
    Posts:
    32
    Hey there,

    I'm trying to play with 2 differrent script with GetComponent but it's really confusing ..


    This is what i wrote


    MoneyConsomable.js
    Code (csharp):
    1. var inv = GameObject.FindGameObjectsWithTag("Inventory");
    2.  
    3. var Valor : int;
    4. var Effect : GameObject;
    5. var pickUp : AudioClip;
    6.  
    7.  
    8.  
    9.  
    10. function OnCollisionEnter ( myCol: Collision ) {
    11.  
    12.   if (myCol.gameObject.tag == "Player"   inv.gameObject.GetComponent(Money).curMoney < inv.gameObject.GetComponent(Money).maxMoney)
    13.  
    14.   {
    15.   inv.gameObject.GetComponent(Money).curMoney += Valor;
    16.   Debug.Log("You got :"+ Valor +"$");
    17.  
    18.   Destroy(this.gameObject);
    19.  
    20.   Instantiate(Effect, transform.position + Vector3(0,5,0), Quaternion.identity);
    21.   audio.PlayOneShot(pickUp);
    22.  
    23.  
    24.  
    25.   }
    26.  
    27. }
    28.  
    Money is attached in "Inventory" gameObject
    MoneyConsomable is attached to the consomable



    I don't know why it don't work

    I also tried with this doc : http://docs.unity3d.com/Documentation/ScriptReference/Component.GetComponent.html
    but it's really hard to understand



    Sorry for my english i'm french



    Edit : Console


    I got these errors :

    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.
    MoneyConsomable..ctor () (at Assets/Script/Consomable/MoneyConsomable.js:1)



    FindGameObjectWithTag can only be called from the main thread.
    Constructors and field initializers will be executed from the loading thread when loading a scene.
    Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
     
    Last edited: Jun 2, 2013
  2. EvilCrown

    EvilCrown

    Joined:
    May 29, 2013
    Posts:
    8
    Try this:

    Code (csharp):
    1.  
    2. var inv : GameObject;
    3. var Valor : int;
    4. var Effect : GameObject;
    5. var pickUp : AudioClip;
    6.  
    7. function Awake() {
    8.     inv = GameObject.FindGameObjectsWithTag("Inventory")[0];
    9. }
    10.  
    11. function OnCollisionEnter ( myCol: Collision ) {
    12.   if (myCol.gameObject.tag == "Player"   inv.gameObject.GetComponent(Money).curMoney <    inv.gameObject.GetComponent(Money).maxMoney)
    13.   {
    14.   inv.gameObject.GetComponent(Money).curMoney += Valor;
    15.   Debug.Log("You got :"+ Valor +"$");
    16.   Destroy(this.gameObject);
    17.   Instantiate(Effect, transform.position + Vector3(0,5,0), Quaternion.identity);
    18.   audio.PlayOneShot(pickUp);
    19.   }
    20. }
    You can also use the Start() method

    Code (csharp):
    1.  
    2. var inv : GameObject;
    3. var Valor : int;
    4. var Effect : GameObject;
    5. var pickUp : AudioClip;
    6.  
    7. function Start() {
    8.     inv = GameObject.FindGameObjectsWithTag("Inventory")[0];
    9. }
    10.  
    11. function OnCollisionEnter ( myCol: Collision ) {
    12.   if (myCol.gameObject.tag == "Player"   inv.gameObject.GetComponent(Money).curMoney <    inv.gameObject.GetComponent(Money).maxMoney)
    13.   {
    14.   inv.gameObject.GetComponent(Money).curMoney += Valor;
    15.   Debug.Log("You got :"+ Valor +"$");
    16.   Destroy(this.gameObject);
    17.   Instantiate(Effect, transform.position + Vector3(0,5,0), Quaternion.identity);
    18.   audio.PlayOneShot(pickUp);
    19.   }
    20. }
     
    Last edited: Jun 2, 2013
  3. EvilCrown

    EvilCrown

    Joined:
    May 29, 2013
    Posts:
    8
    Updated my first post.
     
  4. Scellow

    Scellow

    Joined:
    Jan 25, 2013
    Posts:
    32
    Thanks for your help !! Worked :)

    I used that :

    Code (csharp):
    1. function Awake ()
    2.  
    3. {
    4.  
    5. inv =  GameObject.Find("Inventory");
    6.  
    7. }
     
  5. EvilCrown

    EvilCrown

    Joined:
    May 29, 2013
    Posts:
    8
    Awesome! :)