Passing by Value or by Reference

Hello guys, I need your help here. !!!

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

void OnGUI()
    {
        if(GUI.Button(new Rect(10,120,100,50), "New Level"))
        {
     

             DeleteItems();
            CreateItems();
            FindItemsObjects();
       }
}

    [HideInInspector]
    public Vector3 blueScale;
    [HideInInspector]
    public Vector3 orangeScale;
    [HideInInspector]
    public Vector3 yellowScale;
    [HideInInspector]
    public Vector3 purpleScale;
    [HideInInspector]
    public Vector3 greenScale;
    [HideInInspector]
    public Vector3 redScale;



    [HideInInspector]
    public Transform blue;
    [HideInInspector]
    public Transform green;
    [HideInInspector]
    public Transform orange;
    [HideInInspector]
    public Transform purple;
    [HideInInspector]
    public Transform red;
    [HideInInspector]
    public Transform yellow;

    private GameObject _blue;
    private GameObject _green;
    private GameObject _orange;
    private GameObject _purple;
    private GameObject _red;
    private GameObject _yellow;


public void FindItemsObjects()
    {
 
        _blue = new GameObject ();
        _red = new GameObject ();
        blue = new GameObject ().transform;
        _blue = GameObject.Find ("Item_bbg(Clone)");
        blue = FindChild ( _blue, "line");
        Debug.Log ("/nBLUE TOOOOO" + _blue);
        _green = GameObject.Find ("Item_gbg(Clone)");
        green = FindChild ( _green, "line");
 
        _orange = GameObject.Find ("Item_obg(Clone)");
        orange = FindChild ( _orange, "line");
 
        _purple = GameObject.Find ("Item_pbg(Clone)");
        purple = FindChild ( _purple, "line");
 
        _red = GameObject.Find ("Item_rbg(Clone)");
        red = FindChild ( _red, "line");
 
        _yellow = GameObject.Find ("Item_ybg(Clone)");
        yellow = FindChild ( _yellow, "line");
 
        blueScale = blue.localScale;
        orangeScale = orange.localScale;
        yellowScale = yellow.localScale;
        purpleScale = purple.localScale;
        greenScale = green.localScale;
        redScale = red.localScale;
    }

    public Transform FindChild(  GameObject parent, string name)
    {
        if (parent.name == name)
        {
            Debug.Log("Parent's Name is same as you are searching for");
            return null;
        }

        Transform pTransform = parent.GetComponent<Transform> ();
        foreach (Transform t in pTransform)
        {
            if (t.name == name)
            {
                return t;
            }
        }
        return null;
    }

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!!!

Please use Code tags. Using code tags properly - Unity Engine - Unity Discussions

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:

private GameObject referencedObject;

    void WhenYouDoSomething () {
        referencedObject = yourReferencedObject;
    }

    void UseReferencedObject () {
        //Use the referenced object
    }

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.

Thank you!

Your issue is because the object doesn’t exist anymore. Look into Don’t Destroy on load: Unity - Scripting API: Object.DontDestroyOnLoad

Here is some more script

public interface ColorMatch
{
    void DoColor(float DamageAmount);
    string GetColorName();

}

public class BlueMaterial : ColorMatch
{

    MenuScript menuScriptObj = GameObject.Find("Menu").GetComponent<MenuScript>();
    EnergyBarScript energyBar = GameObject.Find("Items").GetComponent<EnergyBarScript>();

    public void DoColor(float DamageAmount)
    {
        menuScriptObj.blueEnergy += DamageAmount;
        menuScriptObj.blueEnergy = Mathf.Clamp (menuScriptObj.blueEnergy, 1, 101);
        energyBar.UpdateEnergyBar (  menuScriptObj.blue, menuScriptObj.blueEnergy, menuScriptObj.blueScale);
    }

    public string GetColorName()
    {
        return "This is BlueMaterial";
    }
}
public void UpdateEnergyBar (  Transform energySprite, float currentEnergy, Vector3 energyScale)
    {
//        if (energySprite == null)
//                        return;

        Debug.Log ("EnergySprite" + energySprite.name);
        energySprite.transform.localScale = new Vector2 (energyScale.x + currentEnergy , 1);
        Debug.Log ("energySprite scale on x axes   " + energySprite.transform.localScale.x);
    }

still getting that error( (

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

When you load a new scene make it look for the objects again, rather than using the references you were already using?

when im debuging in this function FindItemObjects

    • Debug.Log (“/nBLUE TOOOOO” + _blue);

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

public void DoColor(float DamageAmount)
    {
        menuScriptObj.blueEnergy += DamageAmount;
        menuScriptObj.blueEnergy = Mathf.Clamp (menuScriptObj.blueEnergy, 1, 101);
        energyBar.UpdateEnergyBar (  menuScriptObj.blue, menuScriptObj.blueEnergy, menuScriptObj.blueScale);
    }

and debug, menuscriptObj.blue it tell me that it’s a null object.

That is what Im doing,

_blue= GameObject.Find ("Item_bbg(Clone)");
blue= FindChild ( _blue, "line");

It looks like the _blue variable is not getting updated for this function

public void DoColor(float DamageAmount)
    {
        menuScriptObj.blueEnergy += DamageAmount;
        menuScriptObj.blueEnergy = Mathf.Clamp (menuScriptObj.blueEnergy, 1, 101);
        energyBar.UpdateEnergyBar (  menuScriptObj.blue, menuScriptObj.blueEnergy, menuScriptObj.blueScale);
    }

I wanna say that it’s still the old (first one) value (item)

why is it not getting set? Is the energy what your trying to set when you change scenes?

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 :smile:,

That’s why Im searching for the specific Item on new level.

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.

using UnityEngine;
using System.Collections;

public class ReferenceShiz : MonoBehaviour {

    private static Transform referencedObject;

    void Update () {
        if(Input.GetKeyUp(KeyCode.Space))
            DoShiz ();
    }

    void DoShiz () {
        GameObject newGO = new GameObject(RandomString (10));
        referencedObject = newGO.transform;
    }

    void ChangeScene () {
        DontDestroyOnLoad(referencedObject.gameObject);

        Application.LoadLevel(1);
    }

    void OnGUI () {
        if(GUI.Button(new Rect(10, 10, 100, 40), "Change Scene"))
            ChangeScene ();

        if(referencedObject)
            GUI.Label(new Rect(10, 60, 100, 40), referencedObject.name);
    }

    private string RandomString(int size) {
        const string _chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

        char[] buffer = new char[size];
      
        for (int i = 0; i < size; i++)
            buffer[i] = _chars[Random.Range(0, _chars.Length)];

        return new string(buffer);
    }
}

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

In your other script how are you getting blue? Post the script.

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.

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.