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

Reload an object

Discussion in 'Scripting' started by AstridLongi, Apr 20, 2020.

  1. AstridLongi

    AstridLongi

    Joined:
    Nov 4, 2019
    Posts:
    15
    HiI have an object that I enable and disable with a button, the problem is that deactivating it also deactivates its scripts (even linerenderer) and it is necessary that when I activate it again the scripts will run again, Im looking for but I can't find a solution, there are any way to restart only one object? I was trying with instantiate function but just create and create more objects with different shapes.
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    When you deactivate a gameobject, its component and children and their components and so on stop running. When activating it again, all above mentioned things should also run again. So:
    this is the desired and intended behavior of deactivating a gameobject, and
    this should happen by default.

    Thus i'm not quite sure what your question or problem actually is. What exactly are you trying to do, what do you do, what happens when you do it, and what do you expect to happen instead?
     
  3. AstridLongi

    AstridLongi

    Joined:
    Nov 4, 2019
    Posts:
    15
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    It could be coded in any number of ways so that it explicitly does not restart once re-enabled. Without seeing code it's hard to say.
     
  5. AstridLongi

    AstridLongi

    Joined:
    Nov 4, 2019
    Posts:
    15
    Im enable and disable my object and their scripst does not reload when I enable again my object.
    Code (CSharp):
    1.  public Button btn1;
    2.     private int _AlreadyClicked = 1;
    3.     public GameObject conic, ellipse;
    4. void Update()
    5.     {
    6.         ellipse.GetComponent<GameObject>();
    7.         if (btn1.interactable != true)
    8.         {
    9.             conic.SetActive(true);
    10.             //ellipse.SetActive(false);
    11.         }
    12. }
    13. public void animOnClick() {
    14.      
    15.         Debug.Log("You have clicked the button!");
    16.         if (_AlreadyClicked%2!=0 ) {
    17.             //SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    18.             conic.SetActive(false);
    19.             ellipse.SetActive(true);
    20.         }
    A video of my problem https://www.loom.com/share/77f8bf6feeee482ebdb7f7bda1d12377
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    What exactly are you attempting to do with this line?

    Code (csharp):
    1. ellipse.GetComponent<GameObject>();
    I can assure you it is not doing anything useful to your cause, as a GameObject is never going to be a component on a GameObject (ellipse), and you're not doing anything with the returned value!

    Meanwhile, put a Debug.Log() inside of Update and also one inside the if statement clause to see what is actually running.

    Remember, if you SetActive(false) a GameObject, ALL scripts on it (including the above if it is located on that inactivated GameObject) will no longer run, and hence they won't be able to turn the object back on. Is that your problem?
     
  7. AstridLongi

    AstridLongi

    Joined:
    Nov 4, 2019
    Posts:
    15
    no, the script is part of a system event, not the gameobject that I am deactivating.