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. Dismiss Notice

Passing by Value or by Reference

Discussion in 'Scripting' started by uanmanarmy, Aug 12, 2014.

  1. uanmanarmy

    uanmanarmy

    Joined:
    Jan 22, 2014
    Posts:
    25
    Hello guys, I need your help here. !!!

    All I want is to find an object after it was Deleted an Istantiated again.

    Code (CSharp):
    1. void OnGUI()
    2.     {
    3.         if(GUI.Button(new Rect(10,120,100,50), "New Level"))
    4.         {
    5.      
    6.  
    7.              DeleteItems();
    8.             CreateItems();
    9.             FindItemsObjects();
    10.        }
    11. }
    12.  
    13.     [HideInInspector]
    14.     public Vector3 blueScale;
    15.     [HideInInspector]
    16.     public Vector3 orangeScale;
    17.     [HideInInspector]
    18.     public Vector3 yellowScale;
    19.     [HideInInspector]
    20.     public Vector3 purpleScale;
    21.     [HideInInspector]
    22.     public Vector3 greenScale;
    23.     [HideInInspector]
    24.     public Vector3 redScale;
    25.  
    26.  
    27.  
    28.     [HideInInspector]
    29.     public Transform blue;
    30.     [HideInInspector]
    31.     public Transform green;
    32.     [HideInInspector]
    33.     public Transform orange;
    34.     [HideInInspector]
    35.     public Transform purple;
    36.     [HideInInspector]
    37.     public Transform red;
    38.     [HideInInspector]
    39.     public Transform yellow;
    40.  
    41.     private GameObject _blue;
    42.     private GameObject _green;
    43.     private GameObject _orange;
    44.     private GameObject _purple;
    45.     private GameObject _red;
    46.     private GameObject _yellow;
    47.  
    48.  
    49. public void FindItemsObjects()
    50.     {
    51.  
    52.         _blue = new GameObject ();
    53.         _red = new GameObject ();
    54.         blue = new GameObject ().transform;
    55.         _blue = GameObject.Find ("Item_bbg(Clone)");
    56.         blue = FindChild ( _blue, "line");
    57.         Debug.Log ("/nBLUE TOOOOO" + _blue);
    58.         _green = GameObject.Find ("Item_gbg(Clone)");
    59.         green = FindChild ( _green, "line");
    60.  
    61.         _orange = GameObject.Find ("Item_obg(Clone)");
    62.         orange = FindChild ( _orange, "line");
    63.  
    64.         _purple = GameObject.Find ("Item_pbg(Clone)");
    65.         purple = FindChild ( _purple, "line");
    66.  
    67.         _red = GameObject.Find ("Item_rbg(Clone)");
    68.         red = FindChild ( _red, "line");
    69.  
    70.         _yellow = GameObject.Find ("Item_ybg(Clone)");
    71.         yellow = FindChild ( _yellow, "line");
    72.  
    73.         blueScale = blue.localScale;
    74.         orangeScale = orange.localScale;
    75.         yellowScale = yellow.localScale;
    76.         purpleScale = purple.localScale;
    77.         greenScale = green.localScale;
    78.         redScale = red.localScale;
    79.     }
    80.  
    81.     public Transform FindChild(  GameObject parent, string name)
    82.     {
    83.         if (parent.name == name)
    84.         {
    85.             Debug.Log("Parent's Name is same as you are searching for");
    86.             return null;
    87.         }
    88.  
    89.         Transform pTransform = parent.GetComponent<Transform> ();
    90.         foreach (Transform t in pTransform)
    91.         {
    92.             if (t.name == name)
    93.             {
    94.                 return t;
    95.             }
    96.         }
    97.         return null;
    98.     }
    99.  



    The problem is that when I start the game It's working it found the objects that I need. Now after Im pressing new Level, the old Items are deleted, the new One are Instantiated and with the help of FindItemObject the new Objects must be found, but unfortunately they are not, cuz it's telling me that the transforms was deleted. So the new objects are not passing to the gameObjects, but the old ones are tracked. So when They were Deleted They cannot be found anymore.

    How I reached this conclusion? , I wasnot deleting the old Items, and when I tested, indeed the action was on the old Items not on the new Items.

    How to pass the action to the new Items, Someone told me to try by reference, but I dont know how please help!!!
     
    Last edited: Aug 12, 2014
  2. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
  3. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    I haven't read your code since it's unreadable without code tags but if I understand correctly to reference a object you'd do something like:

    Code (CSharp):
    1. private GameObject referencedObject;
    2.  
    3.     void WhenYouDoSomething () {
    4.         referencedObject = yourReferencedObject;
    5.     }
    6.  
    7.     void UseReferencedObject () {
    8.         //Use the referenced object
    9.     }
     
  4. uanmanarmy

    uanmanarmy

    Joined:
    Jan 22, 2014
    Posts:
    25
    MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it.
    Your script should either check if it is null or you should not destroy the object.

    That is my error when moving to next level.
     
  5. uanmanarmy

    uanmanarmy

    Joined:
    Jan 22, 2014
    Posts:
    25
  6. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
  7. uanmanarmy

    uanmanarmy

    Joined:
    Jan 22, 2014
    Posts:
    25
    Here is some more script

    Code (CSharp):
    1.  
    2.  
    3. public interface ColorMatch
    4. {
    5.     void DoColor(float DamageAmount);
    6.     string GetColorName();
    7.  
    8. }
    9.  
    10. public class BlueMaterial : ColorMatch
    11. {
    12.  
    13.     MenuScript menuScriptObj = GameObject.Find("Menu").GetComponent<MenuScript>();
    14.     EnergyBarScript energyBar = GameObject.Find("Items").GetComponent<EnergyBarScript>();
    15.  
    16.     public void DoColor(float DamageAmount)
    17.     {
    18.         menuScriptObj.blueEnergy += DamageAmount;
    19.         menuScriptObj.blueEnergy = Mathf.Clamp (menuScriptObj.blueEnergy, 1, 101);
    20.         energyBar.UpdateEnergyBar (  menuScriptObj.blue, menuScriptObj.blueEnergy, menuScriptObj.blueScale);
    21.     }
    22.  
    23.     public string GetColorName()
    24.     {
    25.         return "This is BlueMaterial";
    26.     }
    27. }
    28.  
    Code (CSharp):
    1.  
    2. public void UpdateEnergyBar (  Transform energySprite, float currentEnergy, Vector3 energyScale)
    3.     {
    4. //        if (energySprite == null)
    5. //                        return;
    6.  
    7.         Debug.Log ("EnergySprite" + energySprite.name);
    8.         energySprite.transform.localScale = new Vector2 (energyScale.x + currentEnergy , 1);
    9.         Debug.Log ("energySprite scale on x axes   " + energySprite.transform.localScale.x);
    10.     }
    11.  
    12.  
     
  8. uanmanarmy

    uanmanarmy

    Joined:
    Jan 22, 2014
    Posts:
    25
    still getting that error( (
     
  9. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    Did you look at the docs? are you making sure the reference isn't being destroyed?

    I'm going to play around with it and get back to you
     
  10. lrlelaldl

    lrlelaldl

    Joined:
    Jul 27, 2014
    Posts:
    75
    When you load a new scene make it look for the objects again, rather than using the references you were already using?
     
  11. uanmanarmy

    uanmanarmy

    Joined:
    Jan 22, 2014
    Posts:
    25
    when im debuging in this function FindItemObjects

    1. Debug.Log ("/nBLUE TOOOOO" + _blue);

      It always found the right one, but after I go here

      Code (CSharp):
      1. public void DoColor(float DamageAmount)
      2.     {
      3.         menuScriptObj.blueEnergy += DamageAmount;
      4.         menuScriptObj.blueEnergy = Mathf.Clamp (menuScriptObj.blueEnergy, 1, 101);
      5.         energyBar.UpdateEnergyBar (  menuScriptObj.blue, menuScriptObj.blueEnergy, menuScriptObj.blueScale);
      6.     }
      and debug, menuscriptObj.blue it tell me that it's a null object.
     
  12. uanmanarmy

    uanmanarmy

    Joined:
    Jan 22, 2014
    Posts:
    25
    That is what Im doing,

    Code (CSharp):
    1. _blue= GameObject.Find ("Item_bbg(Clone)");
    2. blue= FindChild ( _blue, "line");
     
  13. uanmanarmy

    uanmanarmy

    Joined:
    Jan 22, 2014
    Posts:
    25
    It looks like the _blue variable is not getting updated for this function

    Code (CSharp):
    1. public void DoColor(float DamageAmount)
    2.     {
    3.         menuScriptObj.blueEnergy += DamageAmount;
    4.         menuScriptObj.blueEnergy = Mathf.Clamp (menuScriptObj.blueEnergy, 1, 101);
    5.         energyBar.UpdateEnergyBar (  menuScriptObj.blue, menuScriptObj.blueEnergy, menuScriptObj.blueScale);
    6.     }
    7.    
    I wanna say that it's still the old (first one) value (item)
     
  14. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    why is it not getting set? Is the energy what your trying to set when you change scenes?
     
  15. uanmanarmy

    uanmanarmy

    Joined:
    Jan 22, 2014
    Posts:
    25
    Yep, I just Have an Prefab with Childs,

    For example for blue I have Item_bbg(clone), now Im searching for line child

    and then After my character will make some damages this line will be increased,

    And for different levels I have the same Items but theyr possition is mixed, (That's why Im deleting and Insantiating again)

    I tryed to Not delete them but just to Autoposition them but I dont really know how :D,

    That's why Im searching for the specific Item on new level.
     
  16. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    Ok here's a quick example I knocked up. It creates a random object. References it. I can then change scene at I still have a reference to the transform.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ReferenceShiz : MonoBehaviour {
    5.  
    6.     private static Transform referencedObject;
    7.  
    8.     void Update () {
    9.         if(Input.GetKeyUp(KeyCode.Space))
    10.             DoShiz ();
    11.     }
    12.  
    13.     void DoShiz () {
    14.         GameObject newGO = new GameObject(RandomString (10));
    15.         referencedObject = newGO.transform;
    16.     }
    17.  
    18.     void ChangeScene () {
    19.         DontDestroyOnLoad(referencedObject.gameObject);
    20.  
    21.         Application.LoadLevel(1);
    22.     }
    23.  
    24.     void OnGUI () {
    25.         if(GUI.Button(new Rect(10, 10, 100, 40), "Change Scene"))
    26.             ChangeScene ();
    27.  
    28.         if(referencedObject)
    29.             GUI.Label(new Rect(10, 60, 100, 40), referencedObject.name);
    30.     }
    31.  
    32.     private string RandomString(int size) {
    33.         const string _chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    34.  
    35.         char[] buffer = new char[size];
    36.      
    37.         for (int i = 0; i < size; i++)
    38.             buffer[i] = _chars[Random.Range(0, _chars.Length)];
    39.  
    40.         return new string(buffer);
    41.     }
    42. }
    43.  
     
  17. uanmanarmy

    uanmanarmy

    Joined:
    Jan 22, 2014
    Posts:
    25
    No still not working ((.

    My question is that Im making a "loop" every Time a create a new Level
    Deleting, Creating and searching for Items.

    Now For first level, everything works nice.

    For the next Level, Im Deleting Creating and Searching again the Items.

    Now As from Code Above in the thread, when Im searching the Items and Debuging.Log, it shows me the right Item, but After calling the blue variable in another script, it shows me that the variable is null.

    So it was deleted (and can't find it).

    I'm confused cuz' Im searching for it no ? and Im assigning the new Searched object to blue variable no ?

    by

    Find the GameObject.
    _blue = GameObject.Find("name");

    //accessing it's child

    blue = FindChild(_blue, "nameofChild")

    and When I debug this 2 variable every time it shows me a result, I mean the GameObject and Child that was Instantiated in the scene.

    But when Im calling the blue variable (founded Children) in another script it shows me that has a null value.

    My question is, How to do to get the right variable (I mean the value), so the value that pas to the variable called in another script to be updated everytime IM searching for GameObjects
     
  18. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    In your other script how are you getting blue? Post the script.
     
  19. uanmanarmy

    uanmanarmy

    Joined:
    Jan 22, 2014
    Posts:
    25
    This is menuscript
    http://pastebin.com/UXVZUUZR


    This is EnergyBarScript
    http://pastebin.com/cLmT9y45

    The error comes in EnergyBarScript Line 10

    In UpdateFEnergyBar.
    Since there will be increased the energySpriteTransform, and dude to it's null value it gives me that

    MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it.
    Your script should either check if it is null or you should not destroy the object.
     
  20. uanmanarmy

    uanmanarmy

    Joined:
    Jan 22, 2014
    Posts:
    25
    Im doing an Match3 Game by the way, So this Items are some Powers for Match3 gems.

    So There are 6 colors, and For every level will be different number of Collors, (Dipending on the number Im putting in the XML file.

    So for example if for first level there will be 6 Items (and 6 types of gems ), for the 2nd Level there will be Only 4 (and 4 Types of Gems), that's Why Im trying to Delete and Instantiate the number of Items. and Search for him.

    So When I will do a match3 with blue Gems, the energyBar from Blue Item will get Increased by some amount of Damage.