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!!!
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.
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);
}
}
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 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.