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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How do I Make this script Re-Enable (ReCheckmark) its self?

Discussion in 'Scripting' started by bonersoup, Jun 2, 2015.

  1. bonersoup

    bonersoup

    Joined:
    Nov 11, 2013
    Posts:
    18
    Ok so this script obviusly destroys the object but it also disables the script when the object respawns...
    Is there a way i can make it enable itself?
    Code (JavaScript):
    1.  
    2. public var treeHealth : int = 5;
    3. var logs : Transform;
    4. var rspawn : Transform;
    5. var tree : GameObject;
    6. var speed : int = 8;
    7. function Start()
    8. {
    9.     tree = this.gameObject;
    10.     GetComponent.<Rigidbody>().isKinematic = true;
    11. }
    12.  
    13. function Update()
    14. {
    15.     if(treeHealth <= 0)
    16.     {
    17.         GetComponent.<Rigidbody>().isKinematic = true;
    18.         GetComponent.<Rigidbody>().AddForce(transform.forward * speed);
    19.         DestroyTree();
    20.     }
    21. }
    22.  
    23. function DestroyTree()
    24. {
    25.     yield WaitForSeconds(7);
    26.     Destroy(tree);
    27.    
    28.     var position : Vector3 = Vector3(Random.Range(-1.0, 1.0), 0, Random.Range(-1.0, 1.0));
    29.     Instantiate(logs, tree.transform.position + Vector3(0,0,0) + position, Quaternion.identity);
    30.     Instantiate(logs, tree.transform.position + Vector3(2,0,0) + position, Quaternion.identity);
    31.     Instantiate(logs, tree.transform.position + Vector3(5,0,0) + position, Quaternion.identity);
    32.     treeHealth = 5;
    33.     Instantiate(rspawn, tree.transform.position + Vector3(0,0,0) + position, Quaternion.identity);
    34.  
    35.  
    36.    
    37. }
     
  2. scippo97

    scippo97

    Joined:
    Jul 22, 2012
    Posts:
    67
    As far as I know, no. You may need to use another script just for this particular purpose.
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    I don;t see anything in there about anything being set to "inactive"... but in general, if the gameobject/component is set to "SetActive(false)" it can't "wake itself up" since it's not running any scripts, you need to set it back to active from "outside" via a reference. Should also note that functions like "Find" and "Get..." skip inactive components for the most part (check the API for specifics for each function).
     
  4. bonersoup

    bonersoup

    Joined:
    Nov 11, 2013
    Posts:
    18
    Theres nothing script wise i think its just default to turn a script on a object if its destroyed... i just need a "Line" of "Code"
    that will re enable this script. the name of the actual file is TreeController.js.
    ive tried:
    enabled=true;
    this.enabled=true;
    cant seem to think of anything else..
    maybe making it re activate via external script?
    how would i do so?
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    the default for something that is destroyed is for to not exist any more, at all... the object instantiated from "rspawn" is something completely different. If that object's script is not active it suggests that the prefab you have linked in the inspector has the script disabled. That is where I suspect your issue lies.
     
  6. bonersoup

    bonersoup

    Joined:
    Nov 11, 2013
    Posts:
    18
    ok so is there something that will just activate the script in general. like a line that will make any object that has this script on it checkmark it enabled????
     
  7. bonersoup

    bonersoup

    Joined:
    Nov 11, 2013
    Posts:
    18
    So Something like this?????
    Code (JavaScript):
    1. if.TreeController.Equals(IsActivated=false;){
    2.      IsActivated=true;
    3. }