Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Why are the inspector values not been overwritten at start?(answered)

Discussion in 'Scripting' started by tawdry, Feb 27, 2015.

  1. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    Here is the code and a picture you can clearly see the values in the script are not at all what are in the inspector and this is after a rebbot and a reinstall of the new unity version.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class magicuser : MonoBehaviour {
    4. public magicspells myScript;
    5.  
    6. GameObject leg;
    7. GameObject body;
    8. GameObject head;
    9. bool combat=true;
    10. public GameObject Lookatme;
    11. string hitTag;
    12. public int ray;
    13. public int hit;
    14. public Vector3 Headhere;// this is not right
    15. public int readthis=100;// this is not right
    16. public int tier=33;// this is not right
    17. public int you=50;// this is not right
    18.  
    19.     void Start () {
    20.  
    21.         leg = GameObject.Find ("elegdam");
    22.         body = GameObject.Find ("ebodydam");
    23.         head = GameObject.Find ("eheaddam");
    24.         //Vector3 mousePos=new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0f);
    25.  
    26.     }
    27.  
    28. //    void OnParticleCollision(GameObject other) {
    29.     //    Debug.Break ();
    30.  
    31.  
    32.  
    33.     //}
    34.    
    35.  
    36.  
    37.    
    38.     void Update(){
    39.                 if (combat == false) {
    40.                         return;
    41.                 }
    42.  
    43.                 //    Vector3 mousePos=new Vector3(Input.mousePosition.x, Input.mousePosition.y,100);
    44.     //    if (Input.GetMouseButtonDown (2)) {
    45.     //            Ray rayz = Camera.main.ScreenPointToRay (Input.mousePosition  );
    46.     //        targets = Camera.main.ScreenToWorldPoint (mousePos );
    47.     //        Debug.Log (targets);    //targets=wordPos;
    48.     //        myScript.firewall ();
    49.     //    }
    50.  
    51.  
    52.  
    53.         // DO DAMAGE FIRE AT SPECIFIC SPOTS
    54.         if (Input.GetMouseButtonDown (1)) {
    55.             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    56.             RaycastHit hit;
    57.             if (Physics.Raycast (ray, out hit, 100)) {
    58.                 float distance = Vector3.Distance (hit.transform.position, transform.position);
    59.                 if (distance < 50) {
    60.                         hitTag = hit.collider.tag;
    61.                         if (hitTag != ("null")) {
    62.                         Lookatme = hit.transform.gameObject;
    63.                         Headhere = hit.collider.transform.position;
    64.                         Debug.Log (hit.collider.transform.position);
    65.                         Debug.Log(Lookatme);
    66.                         myScript.fireball ();
    67.  
    68.                            
    69.                                 }
    70.                         }
    71.                 }
    72.  
    73.             }
    74.  
    75.    
    76.     }}
    77.  

    https://www.flickr.com/photos/129989537@N04/16474583769/

    This has been pissing me off for 2 whole days now
     
  2. KyleOlsen

    KyleOlsen

    Joined:
    Apr 3, 2012
    Posts:
    237
    Public variables will only use their initialize value when first applied to an object. You can reset this by making the incorrect variables private, doing a recompile, then setting them public again.
     
  3. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    hmm always thought once you hit play it overwrites the inspector.. Main issue i'm having is that i have a instantiated object that has a reference to this script via a drag and drop and it only uses the values in the initialization and ignores any changes made in start or update but if i edit the editor during runtime it uses those values. makes no sense to me..
     
  4. KyleOlsen

    KyleOlsen

    Joined:
    Apr 3, 2012
    Posts:
    237
    Sorry, that's incorrect. The inspector is designed to easily change serialized values on the fly without having to dig into your code.
     
  5. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    yep i get the on the fly bit but when u close our project or reboot your system bit crazy it keeps the values and doesnt have initialize reset them.
     
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,514
    it's not supposed to.

    The default values are just for when you first time add the component.

    If you modify them in the inspector during edit time, they stay the modified value. They're supposed to do this. This way you can add a component to multiple gameobjects and configure them differently.

    Say you have a HealthScript, and it contains a field for 'MaxHealth', and you add it to your player and to the bad guy, and the player gets a maxhealth of 10, but the bad guy a max health of 20. How would you do that if the value kept getting reset?
     
  7. KyleOlsen

    KyleOlsen

    Joined:
    Apr 3, 2012
    Posts:
    237
    That's the idea of serialized data, to keep it around. You can tag the variable with [NonSerialized] and I believe it will work like you're thinking.
     
  8. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You can also reset the component in the inspector.
     
  9. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    I understand that but if you have a line in the code that says maxhealth=15; and you change the maxheath to 20 in the inspector i just thought that when u run it again the maxhealth would revert to the scripted value not a experimental value u might have benn playing around witth.
     
  10. gcoope

    gcoope

    Joined:
    Dec 20, 2012
    Posts:
    11
    If you want values to always start the same then set them in your Start() function, they will always be set to that value whenever the scene is run.
     
  11. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    First time really working with public values so least i know whats up from here.