Search Unity

Change Sprite Based on Menu Selection

Discussion in '2D' started by j0hns0usa, May 3, 2017.

  1. j0hns0usa

    j0hns0usa

    Joined:
    Apr 2, 2017
    Posts:
    43
    I need to set a new print statement to make sure it's passing both variables.
     
  2. j0hns0usa

    j0hns0usa

    Joined:
    Apr 2, 2017
    Posts:
    43
    Plus, I don't want it to create a new one, I just want it to update the existing..
     
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,713
    Okay, I was wondering if that's why you were asking.
    If one is already there, simply make a reference to that in whatever script you're talking about.
    Then apply the sprite to that game object.. and well, obviously if it already has a sprite renderer, there's no need to add another. :)
     
  4. j0hns0usa

    j0hns0usa

    Joined:
    Apr 2, 2017
    Posts:
    43
    I've been trying to find a way to reference the already existing one, do you know where/how to do it?
     
  5. j0hns0usa

    j0hns0usa

    Joined:
    Apr 2, 2017
    Posts:
    43
    Think I found how to reference it.

    BTW, I really do appreciate all your help.
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,713
    Just to be clear, is this gameobject only in the second scene?
    If that's the case, just put "public GameObject PlayerOne;" in the script and drag the object from the hierarchy to the script there.
     
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,713
    :) np.. hopefully everything is almost working for ya. heh
     
  8. j0hns0usa

    j0hns0usa

    Joined:
    Apr 2, 2017
    Posts:
    43
    Yes, the gameObject that I want to modify the Sprite asset for exists only in the second scene.
     
  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,713
    Cool, that will work then.
     
  10. j0hns0usa

    j0hns0usa

    Joined:
    Apr 2, 2017
    Posts:
    43
    If my gameObject is labeled as: player01, do I write it like this: "public GameObject player01;" ?
     
  11. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,713
    You can actually name the variable anything you want. All that matters is that you drag the right object into the link in the inspector :)
     
  12. j0hns0usa

    j0hns0usa

    Joined:
    Apr 2, 2017
    Posts:
    43
    Like this:
    Code (CSharp):
    1. public GameObject PlayerOne;
    2. public class playerOneGet : MonoBehaviour {
    3.  
    4.     void Start() {
    5.  
    6.         if (PlayerPrefs.HasKey ("Player One")) {
    7.             if (PlayerPrefs.GetInt ("Player One") == 1) {
    8.                 Sprite devils1 = Resources.Load ("Devils1", typeof(Sprite)) as Sprite;
    9.                 SpriteRenderer rend = player01.AddComponent(typeof(SpriteRenderer)) as SpriteRenderer;
    10.                 rend.sprite = devils1;
     
  13. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,713
    Sure, you could even add the spriterenderer to the scene object and then just assign the sprite without adding the component..
    Does it work? You keep forgetting to say that? lol
    If it works, great, if not, what's wrong? :)
     
  14. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,713
    btw, nothing wrong with doing:
    Code (csharp):
    1.  
    2. If(PlayerPrefs.HasKey("Player One")) {
    3.    int team = PlayerPrefs.GetInt("Player One");
    4.    switch(team) {
    5.       case 1: // devils
    6.       case 2: // etc...
    7.       }
    8.   }
    9. // sorry didn't mean to complicate , again.. :)
    10.  
     
  15. j0hns0usa

    j0hns0usa

    Joined:
    Apr 2, 2017
    Posts:
    43
    So far, nothing is working in the second scene. I am having a script issue with referring to the gameobject..
     
  16. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,713
    oh hey, if you wrote your script exactly as you pasted it, you're using "player01" instead of PlayerOne. Change that.
     
  17. j0hns0usa

    j0hns0usa

    Joined:
    Apr 2, 2017
    Posts:
    43
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class playerOneGet : MonoBehaviour {
    6.  
    7.     public gameObject player01;
    8.  
    9.     void Start() {
    10.  
    11.         int p1 = PlayerPrefs.GetInt("Player One");
    12.         print("p = " + p1);
    13.  
    14.         if (PlayerPrefs.HasKey ("Player One")) {
    15.             if (PlayerPrefs.GetInt ("Player One") == 1) {
    16.                 Sprite devils1 = Resources.Load ("Devils1", typeof(Sprite)) as Sprite;
    17.                 SpriteRenderer rend = player01.AddComponent(typeof(SpriteRenderer)) as SpriteRenderer;
    18.                 rend.sprite = devils1;
    19.             }
    20.         }
    21.         if (PlayerPrefs.HasKey ("Player One")) {
    22.             if (PlayerPrefs.GetInt ("Player One") == 2) {
    23.                 Sprite rangers1 = Resources.Load ("Rangers1", typeof(Sprite)) as Sprite;
    24.                 SpriteRenderer rend = player01.AddComponent (typeof(SpriteRenderer)) as SpriteRenderer;
    25.                 rend.sprite = rangers1;
    26.             }
    27.         }
    28.         if (PlayerPrefs.HasKey ("Player One")) {
    29.             if (PlayerPrefs.GetInt ("Player One") == 3) {
    30.                 Sprite flyers1 = Resources.Load ("Flyers1", typeof(Sprite)) as Sprite;
    31.                 SpriteRenderer rend = player01.AddComponent (typeof(SpriteRenderer)) as SpriteRenderer;
    32.                 rend.sprite = flyers1;
    33.             }
    34.         }
    35.         if (PlayerPrefs.HasKey ("Player One")) {
    36.             if (PlayerPrefs.GetInt ("Player One") == 2) {
    37.                 Sprite islanders1 = Resources.Load ("Islanders1", typeof(Sprite)) as Sprite;
    38.                 SpriteRenderer rend = player01.AddComponent (typeof(SpriteRenderer)) as SpriteRenderer;
    39.                 rend.sprite = islanders1;
    40.             }
    41.         }
    42.     }
    43. }
     
  18. j0hns0usa

    j0hns0usa

    Joined:
    Apr 2, 2017
    Posts:
    43
    I'm getting an error with that script though for the "public gameObject player01;"
    upload_2017-5-7_13-56-45.png
     
  19. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,713
    Change to "GameObject"

    GameObject = class
    gameObject = local GameObject instance.
    if that makes sense.
     
  20. j0hns0usa

    j0hns0usa

    Joined:
    Apr 2, 2017
    Posts:
    43
    Code (CSharp):
    1.  
    2.     public GameObject player01;
    3.  
    4.     void Example () {
    5.         player01 = GameObject.Find ("player01");
    6.     }
    7.  
    8.     void Start() {
    9.  
    10.         int p1 = PlayerPrefs.GetInt("Player One");
    11.         print("p = " + p1);
    12.  
    13.         if (PlayerPrefs.HasKey ("Player One")) {
    14.             if (PlayerPrefs.GetInt ("Player One") == 1) {
    15.                 Sprite devils1 = Resources.Load ("Devils1", typeof(Sprite)) as Sprite;
    16.                 SpriteRenderer rend = player01.AddComponent(typeof(SpriteRenderer)) as SpriteRenderer;
    17.                 rend.sprite = devils1;
    18.             }
    19.         }
     
  21. j0hns0usa

    j0hns0usa

    Joined:
    Apr 2, 2017
    Posts:
    43
    Gettin' somewhere:
    upload_2017-5-7_14-7-29.png
     
  22. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,713
    You crack me up, forgot to say if that's working or not, again :)
     
  23. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,713
    Okay, so instead of the line to addcomponent do:
    Code (csharp):
    1.  
    2. player01.GetComponent<SpriteRenderer>().sprite = devils1;
    3.  
    that's enough. :)
     
  24. j0hns0usa

    j0hns0usa

    Joined:
    Apr 2, 2017
    Posts:
    43
    Yes, it's working in that, if I select the second team, it prints: "p = 2"
    Then it moves on looking to do something with the component, but it's having an issue there. If I change it to what you are saying, should it look like this?
    Code (CSharp):
    1. if (PlayerPrefs.HasKey ("Player One")) {
    2.             if (PlayerPrefs.GetInt ("Player One") == 1) {
    3.                 Sprite devils1 = Resources.Load ("Devils1", typeof(Sprite)) as Sprite;
    4.                 //SpriteRenderer rend = player01.AddComponent(typeof(SpriteRenderer)) as SpriteRenderer;
    5.                 //rend.sprite = devils1;
    6.                 player01.GetComponent<SpriteRenderer>().sprite = devils1;
     
  25. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,713
  26. j0hns0usa

    j0hns0usa

    Joined:
    Apr 2, 2017
    Posts:
    43
    Bro! It's working!
     
  27. j0hns0usa

    j0hns0usa

    Joined:
    Apr 2, 2017
    Posts:
    43
    Man, if I could, I would buy you a few beers..

    I really can't thank you enough..
     
  28. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,713
    Hey, cool, I'm glad it's all working :)
     
    j0hns0usa and vakabaka like this.
  29. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,017
    :D live chat