Search Unity

Find Image

Discussion in 'Scripting' started by DomDomDom, Mar 5, 2015.

  1. DomDomDom

    DomDomDom

    Joined:
    Jan 21, 2015
    Posts:
    43
    I'm trying to find an image within my hierarchy every time the player respawns.

    What I've got is this:
    Code (csharp):
    1.  
    2. if(damageImage == null) { //If the damageImage was not set because the player spawned in as a prefab; link it.
    3. damageImage = GameObject.FindGameObjectWithTag("DamageImage") as Image;
    4. Debug.Log("The damageImage was not found and has been linked to the player");
    5. } else {
    6. Debug.Log("The damageImage was found and linked to the player");
    7. }
    8.  
    However I get this error: Cannot convert type `UnityEngine.GameObject' to `UnityEngine.UI.Image' via a built-in conversion

    Unless there is a better way to have a gameObject (in my case an image) load into a prefab every time it is instantiated, I'm a little stumped with this.
     
  2. Strategos

    Strategos

    Joined:
    Aug 24, 2012
    Posts:
    255
    Code (CSharp):
    1.  
    2.  
    3. GameObject imageObject = GameObject.FindGameObjectWithTag("DamageImage");
    4.  
    5. if(imageObject != null)
    6. {
    7.    damageImage = imageObject.GetComponent<Image>();
    8. }
    9.  
    10.  
    11.  
     
    schcui likes this.
  3. DomDomDom

    DomDomDom

    Joined:
    Jan 21, 2015
    Posts:
    43
    Thank you, I appreciate your help!
     
  4. knr_

    knr_

    Joined:
    Nov 17, 2012
    Posts:
    258
    Hi DomDomDom,

    Do you delete the game object that has the script referencing the image when a player dies and make a new one when the player is respawned?

    A better way might be to keep the player game object instead of destroying it (avoiding memory fragmentation), create a delegate (callback) and an event that is associated with the delegate, then register the delegate (callback) in all the classes that would want to know about the respawn. In the callbacks you can then reset the existing player game object to the default values.

    Example:


    Code (CSharp):
    1. public class Player : MonoBehaviour
    2. {
    3.    Image m_DamageImage;
    4.  
    5.    void Awake()
    6.    {
    7.       // Initialize player values
    8.       InitializeValues();
    9.  
    10.       // Add an event handler for the Spawn Manager's respawn event
    11.       SpawnManager.Respawn += SpawnManager_OnRespawn;
    12.    }
    13.  
    14.    void OnDestroy()
    15.    {
    16.       // Remove the event handler for the Spawn Manager's respawn event
    17.       SpawnManager.Respawn -= SpawnManager_OnRespawn;
    18.    }
    19.  
    20.    // Event handler for the Spawn Manager's respawn event
    21.    void SpawnManager_OnRespawn()
    22.    {
    23.       InitializeValues();
    24.    }
    25.  
    26.    // Initialize player values
    27.    void InitializeValues()
    28.    {
    29.       // Just a demonstration of what you might put in the Respawn event handler.
    30.       // If you are not deleting the game object that has this script on it then the
    31.       // reference to m_DamageImage should not be null here.
    32.       if (m_DamageImage == null)
    33.       {
    34.          m_DamageImage = GameObject.FindGameObjectWithTag("DamageImage");
    35.       }
    36.    }
    37. }
    38.  
    39. // The spawn manager
    40. public class SpawnManager
    41. {
    42.       // Declare the Respawn event and what a Respawn event handler (delegate) looks like
    43.       public delegate void OnRespawn();
    44.       public static event OnRespawn Respawn;
    45.  
    46.       // Static function that will send a respawn event if there is at least one Respawn
    47.       // event handler registered (see example Player class where it is added (+=) and
    48.       // removed (-=))
    49.       public static void SendRespawnEvent()
    50.       {
    51.          if (Respawn!= null)
    52.          {
    53.             Respawn();
    54.          }
    55.       }
    56. }
    57.  
    58. public class SomeClass
    59. {
    60.    public void SomeFunction
    61.    {
    62.       // Test sending respawn event
    63.       SpawnManager.SendRespawnEvent();
    64.    }
    65. }
    66.  
     
    Last edited: Mar 5, 2015
  5. DomDomDom

    DomDomDom

    Joined:
    Jan 21, 2015
    Posts:
    43
    Thank you for your advice rnakrani! I'll definitely work on implementing this into my existing code. This seems like a much better way to do what I'm doing.

    Currently I destroy the player gameObject when it loses all health. Then I have a gameMaster script that instantiates in the player to a spawn point, which is where I lose the reference to damageImage. I was having a similar problem with my options menu keeping the settings the same from scene to scene.
     
  6. knr_

    knr_

    Joined:
    Nov 17, 2012
    Posts:
    258
    Awesome! Glad to hear it.

    As far as persisting information from one scene to another, the way I like to do that is to have a singleton that doesn't derive from MonoBehaviour.

    The basic idea of a Singleton is this: It gets created the first time you try to access it; its not added to any scene, it exists outside any scene but within the memory allocated for the game. Thus, if you create a Singleton once its created you can access it from anywhere and from any scene during the lifetime of the application. Singletons are where I put data that I need across scenes. I can throw some code up if you like.
     
  7. DomDomDom

    DomDomDom

    Joined:
    Jan 21, 2015
    Posts:
    43
    If you could provide an example, I would truely appreciate that. Thank you for your help!