Search Unity

Referencing Prefab script variables

Discussion in 'Editor & General Support' started by HungryCatGames, Jan 2, 2021.

  1. HungryCatGames

    HungryCatGames

    Joined:
    Jun 24, 2020
    Posts:
    40
    Not sure if this is the correct approach however what I'm looking at is many prefabs (one for each thing in my game) and each one having an attached script as they will do different things. For example a car prefab with a car script which does movement. A mailbox prefab with a mailbox script which handles if a letter is placed in (on) it. A light prefab with a script which handles when to turn the light on or off. Hopefully that gets the idea across. The player gets to place as many of these objects as they can afford. However when the player is placing a light or a mailbox the amount of space on the playing field that each uses varies substantially. So part of the script of each item also contains things like the width and length (in playing field squares) that the object needs.

    The idea was to instantiate an object, move it around with the mouse and the click to place it. I've got this working just fine. However the movement "grid" assumes all objects are the same size right now. So what I want is to instantiate the object, get the L & W from it and then feed that back into the mouse movement "grid" so I can allow for finer placement on smaller objects and coarser placement on larger objects and eventual checking to see if it will fit in the chosen location.

    In the script of the object I defined these basic variables as "public static float sizeL" and sizeW. Normally I can direct reference them by calling the script name and then the variable name. GlobalVariables.TimeOfDay for example of I was storing the timeofday this way.

    So I've declared a gameobject. I instantiate it. (works to this point) I then try MyNewGameObject.sizeL however sizeL is not exposed and not something that I seem to be able to get.

    Am I going about this incorrectly? If this is a common practice approach, then what might I be doing wrong that I can't pull the object(prefab) script variable (or perhaps I should say constant as these will only be read)?