Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

GetComponent C#

Discussion in 'Scripting' started by solarcraft, Nov 18, 2015.

  1. solarcraft

    solarcraft

    Joined:
    Nov 8, 2015
    Posts:
    3
    I'm trying to add a coin to my game. If the coin isn't touched then the level wont be able to switch until the player touches the coin. How do I fix my scripts, my scripts are trying to set a value in a variable then when the value increases to 1 then it allowed the level to change.

    This is all in Unity

    Script 1, titled as "Coin"

    using UnityEngine;
    using System.Collections;

    publicclassCoin:MonoBehaviour{publicGameObject destroyCoin;publicstaticint coinWorth =0;

    voidOnCollisionEnter(Collision other){if(other.transform.tag =="Coin"){Destroy(destroyCoin);
    coinWorth =1;}}}
    Script 2, using variables from the first script

    using UnityEngine;
    using System.Collections;

    publicclassGameManager4:MonoBehaviour{

    Coin coinValue =GetComponent<Coin>().coinWorth;

    voidUpdate(){
    coinValue =Coin.coinWorth;}voidOnCollisionEnter(Collision other){if(other.transform.tag =="Complete"&& coinValue >0){Application.LoadLevel(1);}}}
     
  2. aLovedHater

    aLovedHater

    Joined:
    Oct 12, 2014
    Posts:
    16
  3. solarcraft

    solarcraft

    Joined:
    Nov 8, 2015
    Posts:
    3
  4. solarcraft

    solarcraft

    Joined:
    Nov 8, 2015
    Posts:
    3
    I just realized my code is hard to read. If i get it formatted better would you be able to revise it for me
     
  5. aLovedHater

    aLovedHater

    Joined:
    Oct 12, 2014
    Posts:
    16
    if you check the first link i posted you it will tell you how to use get component, try checking out that and watching the video. it till tell you that if you want to use a variable from your first script in your second script, you have to cast it and get the component from script one. But that only works if both scripts are on the same Gameobject. if not, you also have to tell the second script what object the first script is attached to. it might look something like this
    Code (CSharp):
    1. Script 1:
    2. //this is the variable we want to use in script 2.
    3. public int numberOfCoins; //for an example, here's a number of coins. note that the variable MUST be public in order for script 2 to be able to access it.
    4.  
    5. public void Start()
    6. {
    7.     //our method that runs on start, this will set the variable of numerOfCoins
    8. numberOfCoins = 1;
    9. }
    Script two (if script 1 and 2 are on the same object)
    Code (CSharp):
    1. script 2:
    2.  
    3. private ScriptOne scriptOne; // here we are taking our first script (using the exact name as how it's called, then casting it to a new name so that we can use it's variables in this script (remember that you don't have to call it scriptOne when you cast it, for all unity cares you can name it as ScriptOne thisIsNotAScript; but normally to keep it more neat you name it as i did.
    4.  
    5. void Start ()
    6. {
    7. //now that we've casted the script, lets actually use GetCompnent so that we can use scriptOne's variables
    8. scriptOne = GetComponent<ScriptOne>();
    9. //here we take our new variable name and finish casting it so that we can use it's variables whenever we want!
    10. }
    11.  
    12. void Update()
    13. {
    14. //might aswell make it spam the console with the coin number showing that it worked..
    15. //it's as simple as this.
    16. Debug.Log(scriptOne.numberOfCoins);
    17. //it's just as if it was our own variable, except we had to add "scriptOne."
    18. //the console should now successfully spam "1"
    19. }
    20. }
    now, if you need to do the same thing but your scripts are on different gameobjects, you have to do something slightly different BUT just as easy, i'll make you find that on your own. check the link: https://unity3d.com/learn/tutorials/modules/beginner/scripting/getcomponent and look for "yetAnotherScript"

    Good luck!
     
    Deleted User likes this.
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148