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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Storing Inspector Values into Globals

Discussion in 'Scripting' started by Pacheon, Nov 28, 2018.

  1. Pacheon

    Pacheon

    Joined:
    Oct 8, 2018
    Posts:
    5
    Hello there,

    I have a simple question:
    How do I store the currently given scale value (or anything else on that GameObject) to a global variable inside a C# script?

    Scenario example:
    Tree growing.
    You place a tree, you scale it to your liking (as big as a house with 2 levels), place it so that it will clip just right beyond surface level and let it scale up, and want to store the X-value (Y and Z become obsolete, since the scale is 1:1:1 / a³) so it can resize itself back to the original height/width.

    I made a script letting trees grow, but I am not aiming to decrease every single tree's current X Y and Z scale one by one (will be impossible by over 1000 of trees in my scene), so I need their current "max growth" size stored automatically by the given transform value.


    I've tried it via "public float maxGrowthInit = transform.localScale.x;" and other ones but I guess that logic I have from MSSQL isn't the same here. :'D
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    To clarify desired purpose:
    • Place tree in edit mode
    • Scale to desired final growth size
    • Press play to enter runtime
    • Tree begins at 0,0,0 scale
    • Tree slowly returns to final size
    Your attempt is pretty close, you just need to assign the value in Start:
    Code (CSharp):
    1. float maxGrowth;
    2.  
    3. void Start () {
    4.    maxGrowth = transform.localScale.x; //Store size here
    5.    transform.localScale = Vector3.zero; //Reset tree to small
    6. }
     
  3. Pacheon

    Pacheon

    Joined:
    Oct 8, 2018
    Posts:
    5
    Not exactly.
    As I said, I made a script which does the growing part already, but I need to store the current scale of the tree gather information about an automated "maxGrowth" function.

    Example:

    Transform values of my current Tree,
    Redwood 15,15,15
    Birch Tree 3,3,3
    ... and so on

    Code (csharp):
    1.  
    2. public class PlantGrowth : MonoBehaviour {
    3.  
    4.     // instead of giving a global like "public float maxGrowth = 999f;"
    5.     // and adjusting it manually in the inspector, it should parse the information
    6.     // like this without my input:
    7.     public float maxGrowthInit = GameObject.localScale.x; // I know that this ain't working, but this is how I think that the logic should be handled by refering to data.
    8.     // which in this case should be: "public float maxGrowthInit = 15f;"
    9.     // or "public float maxGrowthInit = 3;" depending on the tree type
    10.     public float speedGrowth = 0.00001f;
    11.     Vector3 initScale;
    12.     Vector3 tempScale;
    13.  
    14.     void Start()
    15.     {
    16.     //Start scale happens here
    17.     }
    18.  
    19.     void Update ()
    20.     {
    21.     //Growing happens here
    22.     }
    23. }
    24.  
    Untitled-1.png
    Instead of manually inputting the 3 and overwriting the placeholder "999"
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    You could achieve this with editor scripting.
    Make an inspectorgui override for PlantGrowth, add HideInInspector to maxGrowthInit, and write to maxGrowthInit in onInspectorGui

    However, I see no reason why you cant just read the value in Start (or awake) if you are beginning runtime with the objects scale at its final grow value (and then settings to small in start)
    [is 'automated "maxGrowth" function' an editorside thing, if so then i get it now - though you could still just read the scale from the transform at that point]
    Code (CSharp):
    1.  
    2. public class PlantGrowth : MonoBehaviour {
    3.     // instead of giving a global like "public float maxGrowth = 999f;"
    4.     // and adjusting it manually in the inspector, it should parse the information
    5.     // like this without my input:
    6.     public float maxGrowthInit = GameObject.localScale.x; // I know that this ain't working, but this is how I think that the logic should be handled by refering to data.
    7.     // which in this case should be: "public float maxGrowthInit = 15f;"
    8.     // or "public float maxGrowthInit = 3;" depending on the tree type
    9.     public float speedGrowth = 0.00001f;
    10.     Vector3 initScale;
    11.     Vector3 tempScale;
    12.     void Start()
    13.     {
    14.     //Read max scale here first
    15.     //Start scale happens here
    16.     }
    17.     void Update ()
    18.     {
    19.     //Growing happens here
    20.     }
    21. }
     
    Last edited: Nov 28, 2018
    Pacheon likes this.