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

my second "if" statements doenst works help me

Discussion in '2D' started by Jessennn, Sep 3, 2018.

  1. Jessennn

    Jessennn

    Joined:
    Aug 6, 2018
    Posts:
    10
    hi , im currently have a problem with my second if statement , im trying using if when changing the pict . but whenever after first if statement works , my second statement if will not working . im trying several solution and not working at all please help

    here my code
    thanks in advanced
    Code (CSharp):
    1.  timer -= Time.deltaTime;
    2.                     if (timer <= 0)
    3.                     {
    4.                         if (uploadimage.sprite = (Sprite)Resources.Load<Sprite>(filepath + "helmimg") as Sprite)
    5.                         {
    6.                             uploadimage.sprite = (Sprite)Resources.Load<Sprite>(filepath + "handphoneimg2") as Sprite;
    7.                             timer = delay;
    8.                             return;
    9.  
    10.                         }
    11.  
    12.                         if (uploadimage.sprite = (Sprite)Resources.Load<Sprite>(filepath + "handphoneimg2") as Sprite)
    13.                         {
    14.                             uploadimage.sprite = (Sprite)Resources.Load<Sprite>(filepath + "helmimg") as Sprite;
    15.                             timer = delay;
    16.                             return;
    17.  
    18.  
    19.                         }
     

    Attached Files:

  2. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,091
    If you want the second if statement to work after the first one you need to remove the "return" statement. A return statement doesn't cause execution to leave the "if" statement. It causes it to leave the function.
     
  3. Jessennn

    Jessennn

    Joined:
    Aug 6, 2018
    Posts:
    10
    hi sir , thankyou for responding ,
    im removing return statement . but its makes even worse , even my first statement not working at all
     
  4. Jessennn

    Jessennn

    Joined:
    Aug 6, 2018
    Posts:
    10
    here my full code
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6. namespace Vuforia
    7. {
    8.     public class datatarget : MonoBehaviour
    9.     {
    10.  
    11.         public Transform TextTargetName;
    12.         public Transform TextDescription;
    13.         public Transform PanelDescription;
    14.         public UnityEngine.UI.Image uploadimage;
    15.         public string filepath = "image/";
    16.         // Use this for initialization
    17.         float timer = 1f;
    18.         float delay = 1f;
    19.         public Sprite helmimg;
    20.         public Sprite stnkimg;
    21.         void Start()
    22.         {
    23.            
    24.         }
    25.         // Update is called once per frame
    26.         void Update()
    27.         {
    28.            
    29.                 StateManager sm = TrackerManager.Instance.GetStateManager();
    30.             IEnumerable<TrackableBehaviour> tbs = sm.GetActiveTrackableBehaviours();
    31.             foreach (TrackableBehaviour tb in tbs)
    32.             {
    33.                 string name = tb.TrackableName;
    34.                 ImageTarget it = tb.Trackable as ImageTarget;
    35.                 Vector2 size = it.GetSize();
    36.                 Debug.Log(" Active image target:" +name + " -size:" +size.x + ","
    37.                 +size.y);
    38.  
    39.                
    40.                 TextTargetName.gameObject.SetActive(true);
    41.                 TextTargetName.GetComponent <Text> ().text = name;
    42.                 uploadimage.gameObject.SetActive(true);
    43.                 TextDescription.gameObject.SetActive(true);
    44.                 PanelDescription.gameObject.SetActive(true);
    45.  
    46.                 //memunculkan desc
    47.                
    48.                
    49.  
    50.                
    51.              
    52.              
    53.                 if (name == "HELM")
    54.                 {
    55.                     TextDescription.GetComponent<Text>().text = "berdasarkan hukum diindonesia setiap pengendara dan penumpang harus menggunakan helm yang berstandar sni." +
    56.                         "helm akan membantu mengurangi luka serius yang mungkin timbul ketika anda terjatuh." +
    57.                         "pada pemakaian helm anda harus memperhatikan kondisi helm. mengikat helm menggunakannya, dan menggunakan pelindung mata dan wajah anda agar tidak silau ketika terkena matahari.";
    58.                     ;
    59.                     timer -= Time.deltaTime;
    60.                      if (timer <= 0)
    61.                     {
    62.                         if (uploadimage.sprite = (Sprite)Resources.Load<Sprite>(filepath + "helmimg") as Sprite)
    63.                         {
    64.                             uploadimage.sprite = (Sprite)Resources.Load<Sprite>(filepath + "handphoneimg2") as Sprite;
    65.                             timer = delay;
    66.                            
    67.                         }
    68.                         if (uploadimage.sprite = (Sprite)Resources.Load<Sprite>(filepath + "handphoneimg2") as Sprite)
    69.                         {
    70.                             uploadimage.sprite = (Sprite)Resources.Load<Sprite>(filepath + "helmimg") as Sprite;
    71.                             timer = delay;
    72.                             return;
    73.                         }
    74.  
    75.                     }
    76.                  
    77.  
    78.                     }
    79.            
    80.                          
    81.  
    82.              
    83.  
    84.                 }
    85.             }
    86. }
    87. }
    88.  
    89.    
    90.  
    91.  
     
  5. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Please post any errors you are getting.
    You appear to have syntax errors in both of your if-statements:
    Code (CSharp):
    1. if (uploadimage.sprite = (Sprite) Resources.Load<Sprite> (filepath + "helmimg") as Sprite)
    2.  
    3. if (uploadimage.sprite = (Sprite) Resources.Load<Sprite> (filepath + "handphoneimg2") as Sprite)
    You are using a single equals sign (which sets values) instead of a double equal sign (which compares values).

    Also, just a fair warning that this code looks very inefficient. You should avoid loading resources and GetComponent calls every frame, as these are heavy processes. Instead, you should cache these values from the Start() or Awake() methods.