Search Unity

Assets/Scripts/LevelMananger.cs(110,15): error CS0122: `Segment.transition' is inaccessible due to i

Discussion in 'Editor & General Support' started by Avirra, Dec 13, 2018.

  1. Avirra

    Avirra

    Joined:
    Dec 1, 2018
    Posts:
    1
    Hello I have a problem with a code and I can not find a solution. Can someone help me?


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class LevelManager : MonoBehaviour
    6. {
    7.     public static LevelManager Instance {set; get;}
    8.     private const bool SHOW_COLLIDER = true;
    9.    
    10.     //Level Spawning
    11.     private const float DISTANCE_BEFORE_SPAWN = 100.0f;
    12.     private const int INITIAL_SEGMENTS = 10;
    13.     private const int MAX_SEGMENTS_ON_SCREEN =  15;
    14.     private Transform cameraContainer;
    15.     private int amountOfActiveSegments;
    16.     private int continiousSegments;
    17.     private int currentSpawnZ;
    18.     private int curentLevel;
    19.     private int y1, y2 , y3;
    20.    
    21.     //List of pices
    22.     public List<Piece> ramps = new List<Piece>();
    23.     public List<Piece> longblocks = new List<Piece>();
    24.     public List<Piece> jumps = new List<Piece>();
    25.     public List<Piece> slides = new List<Piece>();
    26.     public List<Piece> pieces = new List<Piece>();//all pices
    27.    
    28.    
    29.     //List of Segments
    30.     public List<Segment> availableSegments = new List<Segment>();
    31.     public List<Segment> availableTransitions = new List<Segment>();
    32.     public List<Segment> segments = new List<Segment>();
    33.    
    34.    
    35.     //gameplay
    36.    
    37.     private bool isMoving = false;
    38.    
    39.     private void Awake ()
    40.     {
    41.         Instance = this;
    42.         cameraContainer = Camera.current.transform;
    43.         currentSpawnZ = 0;
    44.         curentLevel = 0;
    45.        
    46.     }
    47.     private void Start()
    48.     {
    49.         for    (int i = 0; i < INITIAL_SEGMENTS; i++)
    50.         GenerateSegment();
    51.     }
    52.     private void GenerateSegment()
    53.     {
    54.         SpawnSegment();
    55.        
    56.         if(Random.Range(0f,1f) < (continiousSegments * 0.25f))
    57.        
    58.         {
    59.             //spawn transition segments
    60.             continiousSegments = 0;
    61.             SpawnTransition();
    62.         }
    63.         else
    64.         {
    65.             continiousSegments++;
    66.         }
    67.     }
    68.     private void SpawnSegment ()
    69.     {
    70.         List<Segment>possibleSeg = availableSegments.FindAll(x => x.beginY1 == y1 || x.beginY2 == y2 || x.beginY3 == y3);
    71.         int id = Random.Range(0, possibleSeg.Count);
    72.         Segment s = GetSegment(id, false);
    73.         y1 = s.endY1;
    74.         y2 = s.endY2;
    75.         y3 = s.endY3;
    76.  
    77.         s.transform.SetParent(transform);
    78.         s.transform.localPosition = Vector3.forward * currentSpawnZ;
    79.  
    80.         currentSpawnZ += s.lenght;
    81.         amountOfActiveSegments++;
    82.         s.Spawn();
    83.     }
    84.     private void SpawnTransition()
    85.     {
    86.         List<Segment> possibleTransition = availableTransitions.FindAll(x => x.beginY1 == y1 || x.beginY2 == y2 || x.beginY3 == y3);
    87.         int id = Random.Range(0, possibleTransition.Count);
    88.         Segment s = GetSegment(id, true);
    89.         y1 = s.endY1;
    90.         y2 = s.endY2;
    91.         y3 = s.endY3;
    92.  
    93.         s.transform.SetParent(transform);
    94.         s.transform.localPosition = Vector3.forward * currentSpawnZ;
    95.  
    96.         currentSpawnZ += s.lenght;
    97.         amountOfActiveSegments++;
    98.         s.Spawn();
    99.     }
    100.    
    101.     public Segment GetSegment(int id, bool transition)
    102.     {
    103.         Segment s = null ;
    104.         s = segments.Find(x => x.SegId == id && x.transition == transition && !x.gameObject.activeSelf);
    105.         if (s == null)
    106.         {
    107.             GameObject go = Instantiate((transition) ? availableTransitions[id].gameObject : availableSegments[id].gameObject) as GameObject;
    108.             s = go.GetComponent<Segment>();
    109.             s.SegId = id;
    110.             s.transition = transition;
    111.  
    112.             segments.Insert(0, s);
    113.         }
    114.         else
    115.         {
    116.             segments.Remove(s);
    117.             segments.Insert(0, s);
    118.         }
    119.         return s;
    120.     }
    121.    
    122.    
    123.     public Piece GetPiece(PieceType pt, int visualIndex)
    124.     {
    125.         Piece p = pieces.Find(x => x.type == pt && x.visualIndex == visualIndex && !x.gameObject.activeSelf);
    126.        
    127.        
    128.         if (p == null)
    129.         {
    130.             GameObject go = null;
    131.             if(pt == PieceType.ramp)
    132.                 go = ramps[visualIndex].gameObject;
    133.             else if (pt == PieceType.longblock)
    134.                 go = longblocks[visualIndex].gameObject;
    135.             else if (pt == PieceType.jump)
    136.                 go = jumps[visualIndex].gameObject;
    137.             else if (pt == PieceType.slide)
    138.                 go = slides[visualIndex].gameObject;
    139.            
    140.             go = Instantiate(go);
    141.             p = go.GetComponent<Piece>();
    142.             pieces.Add(p);
    143.         }
    144.        
    145.        
    146.         return p;
    147.     }
    148. }
    149.  
     
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Please post the full error message. Is the code that you are showing LevelManager.cs? The linenumber in the error (115) does not agree with the provided code. Did you copy this code from somewhere?
     
  3. Achie1

    Achie1

    Joined:
    Jan 14, 2018
    Posts:
    26
    Hi! My Question is about the line 132 : go = ramps[visualIndex].gameObject
    Can someone explain me why the ".gameobject" at the end of the line? Can we replace it by "as gameObject"?
     
  4. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    No, as you've probably already tried. Where did you get this code? Where do you fill the ramps list.
     
  5. Achie1

    Achie1

    Joined:
    Jan 14, 2018
    Posts:
    26
    It's from Avirra. He asks a question about it on december 2018.
    I didn't have explanation about the line I didn't understand. Explain it to me please.
     
  6. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    .gameobject is a specific member of the ramps collection. It is a specific property of an object in the collection. May I ask, why are you asking?
     
  7. Achie1

    Achie1

    Joined:
    Jan 14, 2018
    Posts:
    26
    I ask because I don't understand this syntax.
     
  8. Achie1

    Achie1

    Joined:
    Jan 14, 2018
    Posts:
    26
    OK, I found it.
    "Piece" is a component. Thereby, "ramps[visualIndex]" will retrieve a reference to the Piece component.

    "go" is declared as a GameObject, so calling "ramps[visualIndex].gameObject" will retrieve the gameObject the component is attached to.

    https://docs.unity3d.com/ScriptReference/Component-gameObject.html

    (Heluim)