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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Trying to enable image!

Discussion in 'Scripting' started by cjschaffner, Nov 30, 2015.

  1. cjschaffner

    cjschaffner

    Joined:
    Sep 24, 2015
    Posts:
    1
    *I am new to programming, so I am sorry if I sound like an absolute noob*
    I am working on a project revolving around creating a star system. I used this website as a baseline on creating it. I removed the unlocking levels portion and am mainly focused on only using the stars. Problem is, I cannot get the star images to enable/disable when I want them to. There are no errors in the console, and I cannot pinpoint the problem.

    Below is the code for the LevelSelectScript attached to the Main Camera. The Debug.Log() even lists the correct image title I wanted enabled ("1star3"), but nothing seems to happen.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.UI;
    5. public class LevelSelectScript : MonoBehaviour {
    6.     //variable which holds the stars obtained  
    7.     private int stars=0;
    8.  
    9.     //Level to load on button click. Will be used for Level button click event
    10.     public void Selectlevel(string level){
    11.         Application.LoadLevel("Level"+level); //load the level
    12.     }
    13.    
    14.     //function to check for the levels locked
    15.     void  SetStars (){
    16.         //loop through the levels of a particular world
    17.         for(int j = 1; j < LockLevel.levels; j++){
    18.             //get the number of stars obtained for that particular level
    19.             //used to enable the image which should be displayed in the World1 scene beside the individual levels
    20.             stars = PlayerPrefs.GetInt("level"+j.ToString() +": stars");
    21.             //enable the respective image based on the stars variable value
    22.             GameObject.Find(j+"star"+stars).GetComponent<Image>().enabled = true;
    23.             // ^^^-problem here?
    24.             Debug.Log(j+"star"+stars);
    25.         }
    26.     }
    27. }
    This is another snippet of code attached to the Player in Level "Level1.1"
    Code (CSharp):
    1. protected void  GetStars (int stars){
    2.        
    3.         //set the playerprefs value of next level to 1 to unlock
    4.         //also set the playerprefs value of stars to display them on the World levels menu
    5.         for(int j = 1; j < LockLevel.levels; j++){
    6.             int i = 0;
    7.             if(currentLevel == "Level"+(i+1).ToString() +"." +j.ToString())
    8.             {
    9.                 PlayerPrefs.SetInt("level"+j.ToString() +": stars",1);
    10.                 //check if the current stars value is less than the new value
    11.                 if(PlayerPrefs.GetInt("level"+j.ToString() +": stars") < stars)
    12.                     //overwrite the stars value with the new value obtained
    13.                     PlayerPrefs.SetInt("level"+j.ToString() +": stars",stars);
    14.             }
    15.             i++;
    16.         }
    17.     }
    Any help would be great! I just have no idea what I am doing wrong. I have been trying for the last few days to find a solution to no avail.
     
  2. DMSolace

    DMSolace

    Joined:
    Nov 30, 2015
    Posts:
    9
    I'm not sure if this would help but ill post it anyways, this is how i cycle through and enable/disable game objects.

    First create a public GameObject array and put all your stars in it.

    Code (CSharp):
    1. public GameObject[] stars;
    2.     private int starIndex;
    3.  
    4.     void CycleStars()
    5.     {
    6.         int disableStar;
    7.         //cycles through your array of stars to see which one is currently active
    8.         for(disableStar = 0; disableStar < stars.Length -1; disableStar++)
    9.         {
    10.             if (stars[disableStar].activeInHierarchy == true)
    11.             {
    12.                 break;
    13.             }
    14.         }
    15.         //disables previous star and enables new star
    16.         stars[disableStar].SetActive(false);
    17.         stars[starIndex].SetActive(true);
    18. }
    19.  
    The variable starIndex would control which star you want to select, but you will have to use an if statement to make sure starIndex does not exceed the length of stars[].length -1.
     
    Last edited: Nov 30, 2015