Search Unity

confused by what i percieve as a inconsistency

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

  1. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,357
    So i asked this before but didn't get a answer so simplified it to try again
    2 scripts first instatiates a object and gives it a heading the other is attached to the instatiated object and deals with slerping it to the heading.But it returns null for Lookatme and (0,0,0) for Headhere but returns the correct value for tier etc ?the instantiate prefab has a imbedded reference to the first script magicuser and spawns with the reference attached..

    Some experiments prove that Nothing in the update(first script) is been read why would this be the case?


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class magicuser : MonoBehaviour {
    4. public int tier=1;//this gets returned correct
    5. public Vector3 Headhere;
    6. public GameObject Lookatme;
    7. string hitTag;
    8. int ray;
    9. int hit;
    10. public GameObject fire;
    11.  
    12.     void Update(){
    13.  
    14.               if (Input.GetMouseButtonDown (1)) {
    15.             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    16.             RaycastHit hit;
    17.             if (Physics.Raycast (ray, out hit, 100)) {
    18.                 float distance = Vector3.Distance (hit.transform.position, transform.position);
    19.                 if (distance < 50) {
    20.                         hitTag = hit.collider.tag;
    21.                         if (hitTag != ("null")) {
    22.                         Lookatme = hit.transform.gameObject;
    23.                         Headhere = hit.collider.transform.position;//this is correct
    24.                         Debug.Log (hit.collider.transform.position);
    25.                         Debug.Log(Lookatme);//this is the correct name
    26.                         Vector3 direction=Headhere - transform.position;
    27.                         Quaternion rotation = Quaternion.LookRotation (direction);
    28.                         Instantiate (fire, transform.position, rotation);
    29.      
    30.                 }
    31.             }
    32.     }
    33.  
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class movepart : MonoBehaviour {
    4.     Vector3 head;
    5.     public magicuser playerScript;
    6.     public void Start()
    7.     {
    8.         Debug.Log (playerScript.Lookatme+"fgdfg"+playerScript.tier);//returns null for lookatme and 1 for tier
    9.              head = playerScript.targets;
    10.         }
    11.  
    12.     public void Update () {
    13.        Debug.Log (head);//return 0,0,0 vector
    14.        transform.position = Vector3.Lerp(transform.position, head , Time.deltaTime);
    15.         }
    16.     }
    The inconsistency occurs in i have another script that references the first script and it can read the values in update. it is not instantiated.
     
    Last edited: Feb 27, 2015
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    I don't think prefabs can include references to any GameObjects outside of the prefab, so the new object created by your Instantiate() call is going to have a null value for playerScript.

    You could fix this by setting the new instance's playerScript field after you instantiate it; something like this:

    Code (CSharp):
    1. GameObject ob = (GameObject)Instantiate (fire, transform.position, rotation);
    2. movepart mp = ob.GetComponent<movepart>();
    3. mp.playerScript = this;
     
    tawdry likes this.
  3. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,357
    Nope it has the script name as it should it would also throw null exceptions errors instead of returning values. For instance when i put values in the inspector for tier or targets during runtime they get read by the instantiated object but it ignores everything else from start and on
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Are you sure it's referencing the correct magicuser?

    You say the movepart is picking up on changes you make to the magicuser in the inspector; when the magicuser changes its Lookatme and Headhere properties in its Update() method, do you see those updated values in the inspector?
     
  5. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,357
    Have only 1 magicuser in scene this is just a test.And its got even worse now the inspector shows 0 for things with a given value and a vector value for something that hasnt yet got 1 this is after i rebooted my computer as well. CRAZY
    In the attached image you can see the unassigned values and the assigned ones I put the return in so the values can't be changed later.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class magicuser : MonoBehaviour {
    4. //public magicspells myScript;
    5. public Vector3 Headhere;
    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 int readthis=1;
    15. public int tier=1;
    16.  
    17.  
    18.     void Start () {
    19.  
    20.         leg = GameObject.Find ("elegdam");
    21.         body = GameObject.Find ("ebodydam");
    22.         head = GameObject.Find ("eheaddam");
    23.         //Vector3 mousePos=new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0f);
    24.  
    25.     }
    26.  
    27. //    void OnParticleCollision(GameObject other) {
    28.     //    Debug.Break ();
    29.  
    30.  
    31.  
    32.     //}
    33.  
    34.  
    35.  
    36.  
    37.     void Update(){
    38.         return;
    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.  
    78.  
    And yes the values in the inspecter do change when i click ie the lookatme and headhere.
    https://www.flickr.com/photos/129989537@N04/16471578730/

    I noticed that if i assign a value in the inspector then resave the prefab those values remain and don't get changed at the startup
    Now changing the value of tier in the inspector is no longer registering on the instantiated object.
     
    Last edited: Feb 27, 2015
  6. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Well, if your script is getting the same values shown in your inspector when you change those values manually at runtime, but values that are different from the ones shown in the inspector when those same values are set by script, then I suppose it has to be a bug in Unity. It's the same field on the same object, whether it's set through the inspector or through script, so if your program is reading that value correctly in one case and not in another then something is wrong at an extremely fundamental level.

    But if I were you, at this point I would probably be quadruple-checking that my tests are really telling me what I think they are, because it would seem much more likely to me that I have made some simple mistake in my debugging attempts.
     
  7. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,357
    Hey man thx for your time here is what i am observing if i put any value in the inspector then save the prefab those values seem to be hardcoded so even though the inspector shows the values changing the instantiate is using the original prefab settings and is ignoring any changes in start or update this isn't normal behavior i don't think?But it only effects the instantiate object the other script referencing the main script reads the changing values.
     
  8. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,357
    And just to kick me while im down this script which used to work is now also broken and returning the hardset value.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class movepart : MonoBehaviour {
    5.    
    6.     Vector3 head;
    7.     //magicuser playerScript;
    8.     public void Start()
    9.     {
    10.         GameObject Mage = GameObject.Find ("mages");
    11.         magicuser playerScript = Mage.GetComponent<magicuser> ();//have to use magicuser
    12.         head = playerScript.Headhere;Debug.Log (playerScript.you);
    13.        
    14.     }
    15.    
    16.     public void Update () {
    17.         transform.position = Vector3.Lerp(transform.position, head , Time.deltaTime);
    18.     }
    19. }
    20.  
     
  9. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Earlier you said that if you change the values in the inspector at runtime that your script picks up those changes. From this it sounds like that's not true--that it's picking up changes you make to the prefab but not picking up changes made to the active instance while it is running.

    That very strongly suggests that your script is not pointing to the instance that you think it is.
     
  10. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,357
    Earlier that was true now its behaving different again as i said Inconsistent. This is what seems to be happening in order to get a reference dragged to the movepart script i have to use the object in my project menu. During runtime editing the object in the project menu (not in the game) changes the values that the instantiate has so instead of using te ingame object with the same name it is using the setting from the one sitting in the project.If that makes sense


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class movepart : MonoBehaviour {
    5.    
    6.     Vector3 head;
    7.     //public magicuser myScript;
    8.     public void Start()
    9.     {
    10.         GameObject Mage = GameObject.Find ("mages");
    11.         var playerScript = Mage.GetComponent<magicuser> ();//have to use magicuser
    12.         head = playerScript.Headhere;
    13.         Debug.Log (playerScript.tier+"head"+playerScript.Headhere+"read"+playerScript.readthis);
    14.         head = playerScript.Headhere;
    15.     }
    16.    
    17.     public void Update () {
    18.  
    19.  
    20.         transform.position = Vector3.Lerp(transform.position,head , Time.deltaTime);
    21.     }
    22. }
    23.  
    The following code causes it to act as it should.
     
  11. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    By "project menu" do you mean your assets folder?

    Dragging a game object into your asset list creates a prefab of that object. That prefab is NOT the same as an instance of that object that is actually in your scene; changes to one will not automatically affect the other. (If the object is still linked to the prefab, then it's possible to synchronize them in the editor, but not at runtime AFAIK.)

    It sounds like your movepart has a reference to a prefab, not to the object that's actually in your scene. Which means everything I said in my very first post is probably correct: you can't give a prefab a reference to a specific object in your scene, so you need to wait until you instantiate the prefab at runtime and then set the reference through script.

    Even if you don't believe me, why don't you try out the code I suggested back in my first post? Just see if it helps.
     
  12. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,357
    Might be possible to do the drag system if it were done via pooling i guess .I really don't understand your script ill plug it in and see what happens.:D. Not a case of not believing you mate but unity sometimes acts like its tripping on shrooms so its Unity i don't always believe.