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

Destroy instantiated object

Discussion in 'Scripting' started by xxlilxjxx, Mar 15, 2015.

  1. xxlilxjxx

    xxlilxjxx

    Joined:
    Jul 21, 2014
    Posts:
    28
    Every time the function ends, I get the error of, "The object of type 'GameObject has been destroyed but you are still trying access it." I want the instantiated object to be destroyed, but instead the one I set as the variable has been. How can I make it to the way I want it?




    ----------------Script--------------
    var prefab : GameObject; //Object I want to clone

    function scrollUp(){
    var BonusXAxis : float = Random.Range(-1.7,0.7); //Start position to go up from
    var curOBJName : String;
    curOBJName = "clickedOBJ_" + count;

    prefab = Instantiate(prefab); //I thought this would get the cloned object
    prefab.name = curOBJName;
    gameObject.Find(curOBJName).GetComponent(TextMesh).text = "+" + ClickAdd.ToString(); //Tried changing the objects text
    for (var BonusYAxis : float = 0.17; BonusYAxis <= 3.69; BonusYAxis+= 0.05){ //Distance going up
    yield WaitForSeconds(0.01); //Time delay to make movement visible
    gameObject.Find(curOBJName).transform.position = Vector3(BonusXAxis, BonusYAxis, -0.2); //Changing position
    GameObject.Destroy(GameObject.Find(curOBJName));
    }
    }
     
  2. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    You are destroying the original prefab and not cloning it at all. Replace your code with the modified version below. Be aware that I have not tested this, but I'm fairly confident it will work.

    ----------------Script--------------
    var prefab : GameObject; //Object I want to clone
    var referencePrefab : GameObject //Store the clone in a different variable from the original prefab.

    function scrollUp(){
    var BonusXAxis : float = Random.Range(-1.7,0.7); //Start position to go up from
    var curOBJName : String;
    curOBJName = "clickedOBJ_" + count;

    referencePrefab = Instantiate(prefab); //I thought this would get the cloned object
    referencePrefab.name = curOBJName;
    gameObject.Find(curOBJName).GetComponent(TextMesh).text = "+" + ClickAdd.ToString(); //Tried changing the objects text
    for (var BonusYAxis : float = 0.17; BonusYAxis <= 3.69; BonusYAxis+= 0.05){ //Distance going up
    yield WaitForSeconds(0.01); //Time delay to make movement visible
    gameObject.Find(curOBJName).transform.position = Vector3(BonusXAxis, BonusYAxis, -0.2); //Changing position
    GameObject.Destroy(referencePrefab); //Destroy the reference directly instead of looking for it by name.
    }
    }

    If this doesn't work, let me know, but I'm pretty sure that it will solve your issue. Cheers. Good luck!
     
  3. xxlilxjxx

    xxlilxjxx

    Joined:
    Jul 21, 2014
    Posts:
    28
    No it doesn't work :c The same problem happened. I appreciate you trying to help though.
     
  4. xxlilxjxx

    xxlilxjxx

    Joined:
    Jul 21, 2014
    Posts:
    28
    That gave the same error. But I noticed now that it actually gives the error on the second run. Not after the end of the function. Just putting it out there if you didn't know your self.
     
  5. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    I will need to see more of your code to give you a better answer. Also, which line is throwing that error? Is it the instantiation statement or a different line?