Search Unity

Seeing if other game object's sprite is a specific sprite

Discussion in 'Scripting' started by artistshc, Jan 2, 2018.

  1. artistshc

    artistshc

    Joined:
    Mar 7, 2015
    Posts:
    70
    This is a stupid question probably, but I've been suffering a brain problem from Lymes...so forgive me.

    I have sprites attached to the other game object and I want to see if this other game object has a specific sprite that is being rendered at the time. I have a player, and there are a few different sprites that can be rendered on it. I want to see if the baby sleeping sprite is the one that is being rendered at the time.

    if(other.gameObject.GetComponent<SpriteRenderer>().sprite == babySleep) {
    //then
    Debug.Log("baby sleep");
    }

    Sorry if my question doesn't make sense.
    Thanks.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    5,058
    So, what issue are you running into? I'm assuming babySleep is a sprite and you've used it to assign to the SpriteRenderer component, but is it not working?
     
    artistshc likes this.
  3. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    757
    Why not just have a state on the character instead and see what it is set at?
     
    artistshc likes this.
  4. artistshc

    artistshc

    Joined:
    Mar 7, 2015
    Posts:
    70
    So my Sprite is on the GameObject called Baby - on the "Baby Sleep" variable (as seen in the picture below) on the BabyManager script.

    But I'm trying to access it from the Glasses.cs script



    if ((other.gameObject.name.Contains("Head")) || (other.gameObject.tag == "Player")) {

    //then collided is true
    collided = true;
    //wait a second to make sure meaning to collide with head
    //yield return new WaitForSeconds(1);
    if (collided){
    //then change to glasses face sprite
    sr.sprite = glassesFace;

    }
    //if sleeping baby then we need to rotate the glasses
    if(other.gameObject.GetComponent<SpriteRenderer>().sprite == babySleep) {
    //then
    Debug.Log("baby sleep");
    }
    }

    I get the error in Unity (Assets/Scripts/Glasses.cs(39,86): error CS0103: The name `babySleep' does not exist in the current context)

    I hope this answers your question. Thank you so much.
     
  5. artistshc

    artistshc

    Joined:
    Mar 7, 2015
    Posts:
    70
    Hi there. It's hard to explain but it's a doll house game. The doll can have clothes and stuff put on him. But if he lays down, he is at a 40 degree angle and I want the clothes to rotate with him. I want to check to see if the sprite is the sleeping baby, so I know to rotate the clothes (which are on a separate script).
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,713
    One option could be to get the other script and call a method on it which checks and rotates?

    Otherwise, I believe your exact current error is that you don't have a reference to 'babySleep' on the script that's calling it, so you could add that there.
     
    artistshc likes this.
  7. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    757
    Ok, but babymanager assigns the sprite yes? So it could just update a state which is easy to read such as an enum.
     
    artistshc likes this.
  8. artistshc

    artistshc

    Joined:
    Mar 7, 2015
    Posts:
    70
    Thank you guys, I got it working with your help! I really appreciate it! Have a good night!
     
  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,713
    Excellent, glad to hear that. :) If you want to, post which option you chose that worked for you.. a sort of "completion" forum message . heh
     
unityunity