Search Unity

How to access the Properties of another game objects Component threw script?

Discussion in 'Scripting' started by Unlimited_Energy, Aug 27, 2015.

  1. Unlimited_Energy

    Unlimited_Energy

    Joined:
    Jul 10, 2014
    Posts:
    469
    Dose anyone know what the code would be for accessing the properties of another game objects component?

    I have a script attached to a Gameobject called GameManager which is an empty game object. button1 when clicked will change another UI game objects source image to THE BUTTONS source image I clicked on by calling a AddToFirstAvailable function on the GameManagers Script.
    I have ANOTHER button1(which is a clickable button that hovers over the UI image) AudioClip property on its Audio Source component to load a sound file from my resource folder BASED on what the Source Image on the UI game object is.

    Ina nutshell, sword button is clicked, it changes the image on UI Image object to a sword and the Button1 on the UI image game objects AudioClip is loaded with Sword1.wav

    my code is not throwing errors but Its not working. i have a feeling the code is wrong. on Button2's onClick, I am calling the AddClip function for Button1s AudioClip...just not working
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections.Generic;
    4.  
    5. public class AudioM : MonoBehaviour
    6.  
    7. {
    8.  
    9.  
    10.  
    11.     public void AddClip (AudioClip audioo )
    12.     {
    13.    
    14.         if (GameObject.FindGameObjectWithTag("Slot1").GetComponent<Image>().sprite.name == "Sword")
    15.         {
    16.    
    17.        
    18.             if (gameObject.name =="Button1")
    19.             {
    20.                  audioo = (AudioClip) Resources.Load("Sword");
    21.             }
    22.    
    23.         }
    24.    
    25.     }
    26. }
    27.  
     
    Last edited: Aug 29, 2015
  2. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    You need to save a reference to the other gameobject and use the reference to access the other gameobjects properties.

    Like this:
    Code (CSharp):
    1. GameObject tmpGO = GameObject.FindGameObjectWithTag("Slot1");
    2.  
    3. if (tmpGO.GetComponent<Image>().sprite.name == "Sword"){
    4.            if (tmpGO.name =="Button1"){
    5.                 audioo = (AudioClip) Resources.Load("Sword");
    6.            }
    7. }
     
    Unlimited_Energy likes this.
  3. Dimwood2011

    Dimwood2011

    Joined:
    Oct 8, 2012
    Posts:
    4
    how do i recode this code in unity 5?

    if(other.collider.tag == ("dart"))
    --------------------------------------------------------------------------------
    I used this code below for now and it did compile but I would like to know how to fix the upper code because i have used it a long time lol...

    if (other.CompareTag("dart"))
    Thanks!!!
     
  4. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    What has been changed with collider.tag in Unity 5?
     
  5. Unlimited_Energy

    Unlimited_Energy

    Joined:
    Jul 10, 2014
    Posts:
    469
    Thank you for the help but nothing is changing the Audioclip on the button.

    OnClick for Button1 I can call the function AudioSource AddClip and assign Sword1.wav and target Button2 and when I press Button 1 it will change button2's audioclip to Sword1.wav This is acessing the AudioSource DIRECTLY on button2. However, When im trying to target Button2 and Call the AddClip Function FROM my script AudioM thats attached to Button2 it does nothing. For some reason the audio source components AudioClip property is not being changed when calling my scripts Function.

    I need it to compare game object Slot1's Source image for Sword and if it is a sword then it changes the audio clip to Sword1.wav.

    if someone can figure out why this is not working you will have solved a HUGE issue for me that I have spent a week on now trying to figure out. Literally 5 hours a day reading and changing code....nothing yet.
     
  6. Dimwood2011

    Dimwood2011

    Joined:
    Oct 8, 2012
    Posts:
    4
    Nevermind I was accidently using OnTriggerEnter() instead of what I intended to use OnCollisionEnter()
    it compiled correctly now........Sorry about that I have just started converting from UnityScript/Javascript to C#
    I will learn on the way :) Thanks Duugu
     
  7. Unlimited_Energy

    Unlimited_Energy

    Joined:
    Jul 10, 2014
    Posts:
    469
    I think there is an issue here: GetComponent<Image>().sprite.name== "Sword". I feel like this is not corect. I think it should be just GetComponent<Image>().sprite == "sword" (since ins Unitys API the sprite variable is "The sprite that is used to render this image.") however I get this error when i try that:

    error CS0019: Operator `==' cannot be applied to operands of type `UnityEngine.Sprite' and `string'
     
  8. Unlimited_Energy

    Unlimited_Energy

    Joined:
    Jul 10, 2014
    Posts:
    469
    This code is not working either.....

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections.Generic;
    4.  
    5. public class AudioM : MonoBehaviour{
    6.  
    7.  
    8.  
    9.     public void AddClip (AudioClip audioo)
    10.     {
    11.  
    12.         GameObject tmpGO = GameObject.FindGameObjectWithTag("Slot1");
    13.         AudioSource audio = GetComponent<AudioSource> ();
    14.  
    15.         if (tmpGO.GetComponent<Image>().sprite.name == "Sword"){
    16.             if (gameObject.tag == "Button1"){
    17.                 audio.clip = (AudioClip) Resources.Load("Sword1");
    18.             }
    19.         }
    20.  
    21.     }
    22. }
     
    Last edited: Aug 29, 2015
  9. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    Any errors?
     
    Unlimited_Energy likes this.
  10. Unlimited_Energy

    Unlimited_Energy

    Joined:
    Jul 10, 2014
    Posts:
    469
    no errors at all.
     
  11. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    Then I guess the script isn't assigned to a gameobject with the tag "Button1"?
    You need to get a reference to the gameobject Button1 too and test like
    Code (CSharp):
    1. if (youReferenceToGameobjectButton1.tag == "Button1")
     
  12. Unlimited_Energy

    Unlimited_Energy

    Joined:
    Jul 10, 2014
    Posts:
    469
    Im almost positive the issue is with resource.load is it being used in the right way?


    This script is attached to Button2. Im sorry, the code was suppose to say button2 and it is in my script. i had switched button1 and 2 writing to you guys on here. But yea Its attached to button 2 and it dose not work. Button 2 is the one that has the Referene Property AudioClip on its Audio Source Component im trying to change.

    I think I might be confused here. Im trying to DIRECTLY ACCESS Button2's Audio Source Component and change the AudioClip. Would I be better off adding a Public Variable AudioClip to my SCRIPT for Button2 and then OnClick on Button2 call the OneShotPlay function? Am I not able to change the AduioClip directly on the AudioSource component because nothing I do is able to access it