Search Unity

Help for appear and disappear codes

Discussion in 'Scripting' started by Meliodass, Jul 3, 2015.

  1. Meliodass

    Meliodass

    Joined:
    Feb 8, 2015
    Posts:
    32
    Help my problem is that my 1st object is still active but if i collide on it the 2nd object will appear and 1st object will disappear and if i collide onto my 2nd object it will disappear too and my 3rd object will appear and so on but i have no idea how to finish it cos im still a beginner at this :( help
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var object1: GameObject;
    4. var object2: GameObject;
    5. var object3: GameObject;
    6. var object4: GameObject;
    7. private var hideshow1 = false;
    8.  
    9. function HideShow(){
    10.    object1.SetActive(!hideshow1);
    11.    object2.SetActive(hideshow1);
    12.    object3.SetActive(hideshow1);
    13.    object4.SetActive(hideshow1);
    14. }
    15. function Start(){
    16.    HideShow();
    17. }
    18. function OnTriggerExit ( Col : Collider){
    19. if(object1 != ){
    20.     hideshow1 = !hideshow1;
    21.      HideShow();
    22.      }
    23.      else if(object2 != ){
    24.      hideshow1 = !hideshow1;
    25.      HideShow();
    26.      }
    27. }
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you're looking at it wrong.

    Break it down to the individual pieces. When you collide with one object it disappears and the next object appears, just those two objects are involved. Just that. You can "chain" the objects together by dragging them into the relevant slots in the inspectors.
     
  3. Meliodass

    Meliodass

    Joined:
    Feb 8, 2015
    Posts:
    32
    LeftyRighty can you give me sample pls? im really not good with this
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. var nextGameObject: GameObject;
    5.  
    6. function OnTriggerExit ( col : Collider)
    7. {
    8. // if we've collided with the player
    9.     if(col.tag == "player")
    10.     {
    11. // check if there is a nextGameObject, to avoid null exception errors
    12.         if(nextGameObject)
    13.         {
    14. // if there is set it active
    15.             nextGameObject.SetActive(true);
    16.         }
    17. // set this gameobject to inactive
    18.         gameObject.SetActive(false);
    19.     }
    20. }
    21.  
    I'm not a unityscript programmer so this might have some syntax issues... but just something like that added to all the objects, you than drag the gameobject you want as the "second" into the "nextGameObject" slot of the first, third into the second, fourth into the third etc. Have everything but the first one set to inactive to start with (the check box top left of the inspector).

    The idea is rather than trying to manage the whole arrangement in one go (as you were) you break it down into small chunks that work on each gameobject in turn.

    You can either leave the last one without anything set, or put the first in there to have them loop around.

    fyi if you use a @Meliodass style link to link another user it sends them an alert... assuming they have alerts turned on :)
     
  5. Meliodass

    Meliodass

    Joined:
    Feb 8, 2015
    Posts:
    32
    Thank you so much Ill try what you just suggested to me :)