Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Script works in level 01 but now 02...

Discussion in 'Scripting' started by Zoneo, Aug 27, 2015.

  1. Zoneo

    Zoneo

    Joined:
    May 1, 2015
    Posts:
    85
    Hey guys,

    my script works in level 01 but it doesnt work in 02. am creating a fairly simple game where the player simply clicks a bunch of squares trying to find the correctly colored square to proceed to the next level. If they get the wrong square, the level will restart.

    Level 01:

    Everything functions perfectly. The level begins and the Win square and Lose square are randomly selected and assigned. I click the red square and my level restarts as it should. I click the green square and it moves me to level 02.

    Level 02:

    After 1 second, 5 prefab balls drop from the sky. A few seconds later, the same event happens. after a set timer, the selector function is called and the balls are assigned their correct roles. Everything appears to be functioning properly. That is, until i actually try to play the level...

    The balls drop according to the timer. The roles are selected according to the timer. the only problem is, is when i click the balls, nothing happens. it is literally the exact some code (Meaning i assigned the SAME code file to BOTH objects. )


    ALSO:

    i only want one green and one red object per level, but as it stands now, i cant seem to get my code to only do one.. its either all of them, or adding one at a time according to the timer (sets 2 then 10 seconds later set an additional 2)

    Id also like to make the non win/lose squares a different color but i cant seem to get the code right. I'll explain a little more once i have the codes posted....

    BLOCKSCRIPT:
    Code (csharp):
    1.  
    2. public class blockScript : MonoBehaviour {
    3.  
    4.     public static int gameScore;
    5.     public Text Score;
    6.  
    7.  
    8.  
    9.     void Start () {
    10.  
    11.         gameScore = 0;
    12.         Score.text = "Score: 0";
    13.  
    14.     }
    15.  
    16.  
    17.  
    18.     void OnMouseDown () {
    19.         Debug.Log ("WORKING");
    20.         gameObject.SetActive (false);
    21.         IncreaseScore ();
    22. //this doesnt happen...
    23.     }
    24.  
    25.     public void IncreaseScore (){
    26.         gameScore += 1;
    27.         Debug.Log (gameScore);
    28.         Score.text = "Score: " + gameScore.ToString();
    29.     }
    30.  
    31.  
    32. }
    FINDER SCRIPT (Selects the win/lose)
    (in level 01 i have TWO DIFFERENT OBJECTS doing this)
    Code (csharp):
    1.  
    2. public class finder : MonoBehaviour {
    3.     private  blockScript [] allBlocks;
    4.     public float timer;
    5.  
    6.  
    7.     void Start () {
    8.         timer = 3.5f;
    9. //how long till the win/lose is selected
    10.         }
    11.     void Update () {
    12.         timer -= Time.deltaTime;
    13.  
    14.         if (timer < 0) {
    15.             Selector ();
    16.             timer = 10;
    17. //This is what i mean by additional 2
    18.         }
    19.     }
    20.  
    21.  
    22.  
    23.  
    24.     void Selector () {
    25.         allBlocks  = FindObjectsOfType<blockScript> ();
    26.      
    27.         blockScript selectedBlock = allBlocks[Random.Range(0, allBlocks.Length)];
    28.         blockScript selectedBlock2 = allBlocks[Random.Range(0, allBlocks.Length)];
    29.      
    30.         GameObject selectedGO = selectedBlock.gameObject;
    31.         GameObject selectedGO2 = selectedBlock2.gameObject;
    32.      
    33.         selectedBlock.enabled =false;
    34.         selectedBlock2.enabled =false;
    35.      
    36.         redBlock block = selectedGO.AddComponent<redBlock> ();
    37.         block.ChangeColor();
    38.         winBlock block2 = selectedGO2.AddComponent<winBlock> ();
    39.         block2.ChangeColor();
    40.  
    41.     }
    42. }
    ColorChanger:
    Code (csharp):
    1.  
    2. public class colorChanger : MonoBehaviour {
    3.  
    4.     public Color green = Color.green;
    5.     public Color blue = Color.blue;
    6.     public Color magenta = Color.magenta;
    7.     public Color white = Color.white;
    8.     public Color yellow = Color.yellow;
    9.     public Renderer rend;
    10.     public int color;
    11.  
    12.  
    13.     void Start () {
    14.         color = Random.Range (0, 4);
    15.  
    16.  
    17.         Debug.Log ("it's running");
    18.         rend.material.color = blue;
    19.         if (color >= 0) {
    20.             rend.material.color = green;
    21.         }
    22.      
    23.         else
    24.          
    25.         if (color >= 1) {
    26.             rend.material.color = blue;
    27.         }else
    28.         if (color >= 2) {
    29.             rend.material.color = magenta;
    30.         }
    31.         else
    32.         if (color >= 3) {
    33.             rend.material.color = white;
    34.         }
    35.         else
    36.         if (color >= 4) {
    37.             rend.material.color = yellow;
    38.         }
    39.      
    40.     }
    41.  
    42.     public void ColorChanger () {
    43.         Debug.Log ("ColorChanger");
    44.     }
    45. }
    Let me know if you need more information.

    Thank you all in advance for any and all of your help!
     
    Last edited: Aug 27, 2015
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you are disabling the scripts:

    Code (csharp):
    1.  
    2.     selectedBlock.enabled =false;
    3.     selectedBlock2.enabled =false;
    4.  
    disabled code doesn't do anything...
     
  3. Zoneo

    Zoneo

    Joined:
    May 1, 2015
    Posts:
    85
    I disabled those following a youtube guide. they are disabling the blockScript on that specific gameobject because new scripts with new functions are being added to those particular objects. It is not very efficient to have them running two different scripts when there is no need. One script makes it disappear and add to score, while the other restarts or proceeds to the next level. Both being very different jobs.

    However, that being said, that still does not answer the question as to why all of my OTHER objects arent doing as they should be, even before the finder's timer runs out.