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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

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:
    79
    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:
    7,140
    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:
    1,035
    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:
    79
    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:
    79
    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,712
    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:
    1,035
    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:
    79
    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,712
    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