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

Referencing a different script

Discussion in 'Scripting' started by Badass99, Jul 31, 2014.

  1. Badass99

    Badass99

    Joined:
    Jul 28, 2014
    Posts:
    6
    Okay I did this about a year ago and I know that I have to use GameObject.Find("..") to find the script. but what I am trying to do, is I am trying to reference a script to check if the player has equipped a piece of armor and to see if that piece of armor is a specific type (Already done this part) But now I need to return a value on my Armor Equip script IF the armor in my inventory script was equipped.

    I have my inventory setup so that if the player equipped a tunic, the Tunic Boolean var named "Equipped" would be returned as True in my Armor script. I just can't remember what order they go in. Any clarification would be appreciated!
     
  2. der_r

    der_r

    Joined:
    Mar 30, 2014
    Posts:
    259
    You can use the function GetComponent to get individual scripts and components from your objects.
     
  3. Badass99

    Badass99

    Joined:
    Jul 28, 2014
    Posts:
    6
    Thanks for the fast reply! My question wasn't very clear, What I was asking is what would a sample script look like to begin the reference?
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    without knowing how/what you've got setup in your code I'm not sure we can answer that :s
     
  5. Darhkuan

    Darhkuan

    Joined:
    Feb 24, 2014
    Posts:
    21
    I just wrote this in the code inserter - but should pretty much work. For a Boolean you wont actually even need the == true part as it can evaluate it by default.

    Code (CSharp):
    1. GameObject myObject;
    2. myObject = GameObject.Find("CharacterGameObject");
    3.  
    4. myObject.GetComponent(<Armor>);
    5.  
    6. if(myObject.Armor.Equipped == True)
    7. {INSERT CODE}
     
  6. Badass99

    Badass99

    Joined:
    Jul 28, 2014
    Posts:
    6
    Well what I have setup is 2 Scripts that talk to each other. 1 Named "Item" which is my games items database. The other is "Inventory" that is connected to the player directly. Within the Inventory script I have it so if the player equips a tunic it puts it in EquipMenu[0] for array slot 0. But if it's Equipped then it also changes my boolean, called "Equipped" to True. When that is finished, I have a third script that I want to check and see if that boolean is true, IF it's true then I want it to change my material renderer for the chest to the armor. (that part I know how to do as well, I just can't remember how to setup the GetComponent function from one script to the other)
     
  7. Badass99

    Badass99

    Joined:
    Jul 28, 2014
    Posts:
    6
    Ahhhhh okay thanks a lot Darhkuan, that's what i was looking for!

    Thanks everyone else for the replies ^_^