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

help in appear and disappear gameobject

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

  1. Meliodass

    Meliodass

    Joined:
    Feb 8, 2015
    Posts:
    32
    Hi guys im clueless how to this and here is it i have a given location spot in the field and i want when i collide on the 1st location and the 2nd location will appear and the rest will not yet appear and so on with the other location and i have declared it as an array
    upload_2015-7-17_0-18-58.png

    i have this other code but i need to attach each to my location gameobject but this sometimes fails me because some location gameobject will appear and not disappear this will happen
    upload_2015-7-17_0-23-36.png

    some object is still active and are not active and heres the script for that appear and disappear
    Code (JavaScript):
    1. #pragma strict
    2. var thisgameObject: GameObject;
    3. var nextGameObj: GameObject;
    4.  
    5. function Start(){
    6.    thisgameObject.SetActive(true);
    7.    nextGameObj.SetActive(false);
    8. }
    9. function OnTriggerExit ( Col : Collider){
    10.     if(Col.tag == "Player"){
    11.         if(nextGameObj){
    12.             nextGameObj.SetActive(true);
    13.      }
    14.       thisgameObject.SetActive(false);
    15.    }
    16.  
    17. }
    im sorry if its really done bad im really a very beginner at this
     
  2. Clockwerktomato

    Clockwerktomato

    Joined:
    Jul 17, 2015
    Posts:
    3
    I think you can use something like this:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LocationManager : MonoBehaviour
    5. {
    6.     public GameObject[] Locations;
    7.  
    8.     private int _locationIndex;
    9.  
    10.     void Start()
    11.     {
    12.         _locationIndex = 0;
    13.     }
    14.  
    15.     public void PrevLocation()
    16.     {
    17.         SwitchLocation(-1);
    18.     }
    19.    
    20.     public void NextLocation()
    21.     {
    22.         SwitchLocation(1);
    23.     }
    24.  
    25.     void SwitchLocation(int delta)
    26.     {
    27.         var newIndex = _locationIndex + delta;
    28.  
    29.         if (newIndex >= Locations.Length || newIndex <= 0)
    30.             return;
    31.        
    32.         DeactivateLocation();
    33.  
    34.         _locationIndex = newIndex;
    35.         ActivateLocation();
    36.     }
    37.  
    38.     void DeactivateLocation()
    39.     {
    40.         Locations[_locationIndex].SetActive(false);
    41.     }
    42.  
    43.     void ActivateLocation()
    44.     {
    45.         Locations[_locationIndex].SetActive(true);
    46.     }
    47.  
    48.  
    49. }
    And then just call locationManager.NextLocation() or locationManager.PrevLocation() from your script.
     
  3. Meliodass

    Meliodass

    Joined:
    Feb 8, 2015
    Posts:
    32
    Hi i`ve tried your sample code i change bec, i want my element 0 in the given gameobject is setactive while the rest are not and when i collide to my gameobject element 0 the next element will be setactive(true) and the rest will be setactive(false)