Search Unity

Getting .Bounds from a list item

Discussion in 'Scripting' started by MadboyJames, Nov 15, 2017.

  1. MadboyJames

    MadboyJames

    Joined:
    Oct 28, 2017
    Posts:
    262
    Hello, I have some code that constrains my camera to a bound box, but I'm having some trouble getting the bounds for the box from my list. Here is the code to constrain my camera
    Code (csharp):
    1.  
    2.  public static void ConstrainCamera(Camera camera, Bounds bounds) // float roomZoom, bool HardBoundaryUp, HardBoundaryLeft, etc.
    3.     {
    4.         float left = camera.ViewportToWorldPoint(new Vector3(0f, 0f, 0f)).x;
    5.         float right = camera.ViewportToWorldPoint(new Vector3(1.0f, 0f, 0f)).x;
    6.         float top = camera.ViewportToWorldPoint(new Vector3(0f, 1.0f, 0f)).y;
    7.         float bottom = camera.ViewportToWorldPoint(new Vector3(0f, 0f, 0f)).y;
    8.  
    9.  
    10.         if (top > bounds.max.y)
    11.         {
    12.             float topDiff = bounds.max.y - top;
    13.             camera.transform.position += new Vector3(0, topDiff, 0);
    14.  
    15.         }
    16.         else if (bottom < bounds.min.y)
    17.         {
    18.             float botDiff = bounds.min.y - bottom;
    19.             camera.transform.position += new Vector3(0, botDiff, 0);
    20.  
    21.         }
    22.  
    23.         if (right > bounds.max.x)
    24.         {
    25.             float rightDiff = bounds.max.x - right;
    26.             camera.transform.position += new Vector3(rightDiff, 0, 0);
    27.  
    28.         }
    29.         else if (left < bounds.min.x)
    30.         {
    31.             float leftDiff = bounds.min.x - left;
    32.             camera.transform.position += new Vector3(leftDiff, 0, 0);
    33.         }
    34.  
    35.     }
    36.  
    and this is what I'm currently trying to use to input the required variables into the method.
    Code (csharp):
    1.  
    2.  private void FixedUpdate()
    3.     {
    4.         currentBoundary = Room.rooms[Room.rooms.Count - 1].GetComponent<BoxCollider2D>().bounds; //I have a public static Bounds in Room for this, but I cannot access it.
    5.         ConstrainCamera(GetComponent<Camera>(), currentBoundary);
    6.     }
    7.  
    I think it's not working because I'm trying to get a specific instance of Room for its bounds. I have greatly appreciated all the help I have received thus far while working on optimizing my camera, although seeing as I seem to get hung up at almost every juncture, I will most probably continue to implore assistance.
     
  2. iamvideep

    iamvideep

    Joined:
    Oct 27, 2017
    Posts:
    118
    what error do you get when you try to get the bounds?
     
  3. MadboyJames

    MadboyJames

    Joined:
    Oct 28, 2017
    Posts:
    262
    So initially, instead of
    Code (csharp):
    1. currentBoundary = Room.rooms[Room.rooms.Count - 1].GetComponent<BoxCollider2D>().bounds;
    I had
    Code (csharp):
    1.  currentBoundary = Room.rooms[Room.rooms.Count - 1].boundaryBox
    The .boundaryBox is a static variable of the boundaries set inside each instance of Room (which is attached to a gameObject with a BoxCollider2D), and I get a "error CS0176: Static member `Room.boundaryBox' cannot be accessed with an instance reference, qualify it with a type name instead" when I try to use it. The way I showed initially just gets the bounds of whatever room was instantiated most recently, and then refuses to move from that room in game.
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Static variables are accessed via the Type name. So "Room.boundaryBox" is the way to get that value. Static variables are shared by all instances of that type. If each instance of Room should have a different boundaryBox, then it shouldn't be static.
     
  5. MadboyJames

    MadboyJames

    Joined:
    Oct 28, 2017
    Posts:
    262
    Oh! that explains a lot actually. I'm now going to go revise 50% of all the static vars I have used (incorrectly) thus far. Learn something new everyday. Yep that was my problem. Thank you! c:
     
    LiterallyJeff likes this.
  6. MadboyJames

    MadboyJames

    Joined:
    Oct 28, 2017
    Posts:
    262
    also your "please use code tags" link was pretty helpful.
     
    Joe-Censored and LiterallyJeff like this.