Search Unity

[SOLVED] Issue when Multiple Scripts reference same Variable?

Discussion in 'Scripting' started by Ziddeth, Aug 29, 2018.

  1. Ziddeth

    Ziddeth

    Joined:
    Aug 29, 2018
    Posts:
    2
    Hi, I'm new at Unity and learning how to script. I've been successfully following a few tutorials and running into an issue that I can't really find an answer for at the moment.

    In one of the videos I watched, I was able to follow along and create a working script that allowed Player and NPC movement across Tiles in a Tactical game setting. In another tutorial, I was also able to create a working Tile Editor that can be accessed through the Inspector Window. Both work, but there is a problem when trying to implement both in the same Project..

    The Tactical script references the variable "Tile" for Height, Distance, and other A* properties.
    The Editor script references "Tile" for Growing and Shrinking selections within the Inspector Window.

    Tactical snippet
    Code (CSharp):
    1.     //Needed for BFS (breadth first search)
    2.     public bool visited = false;
    3.     public Tile parent = null;
    4.     public int distance = 0;
    Editor snippet
    Code (CSharp):
    1.     [HideInInspector] public Tile prev;
    2.     [HideInInspector] public int distance;
    3.  
    4.     public void Grow()
    Both scripts define the "Tile" variable, and reference it across their methods in different ways. This is causing both scripts to break when placed in the same project. I don't think I can switch them from Public to Private because both Tactical and Editor scripts rely on a few other scripts referencing them in order to work.. Or if I can switch some to Private, I wouldn't know specifically which to do. Is there a way to manage similarly named variables to work separately of each other?

    TL;DR, How do I get two Scripts that work independently to work together if they reference similar variables for different purposes?
     
  2. kru

    kru

    Joined:
    Jan 19, 2013
    Posts:
    452
    namespaces can be used to separate similarly named classes from colliding. eg one of your tiles is Tactical.Tile while the other is Selectables.Tile

    Another option, which you might strongly consider, is to name your classes to get a more detailed view of their purpose. Tile is a pretty broad term. It only implies that its part of an ordered whole. You could add an adjective which describes the purpose of the tile, eg MapTile or SelectionTile
     
    Ziddeth likes this.
  3. Ziddeth

    Ziddeth

    Joined:
    Aug 29, 2018
    Posts:
    2
    Thank you for this, I had just now discovered Namespaces and was wondering if it was the correct fix. I will definitely go through and name the classes to be more descriptive. [:
     
  4. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,998
    To kind of dumb down what kru wrote, it's just an unlucky coincidence they happened to use the same name. But the names are just arbitrary and can be anything. Change one of them. Can even use Search&Replace in each project. TacticalTile and EditTile would work fine. TileA and TileB -- also fine.

    Sure, namespaces are another way to solve this. But it's like getting a file cabinet to store 2 pieces of paper.

    Not a big deal, but might be good to know, Tile is a _type_, not a variable. They are probably defined as classes, which are types. parent and prev are the variables.
     
    Ziddeth likes this.