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

here are any way to restart only one object by code?

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

Thread Status:
Not open for further replies.
  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. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,337
    Joe-Censored likes this.
  3. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,519
    When you say "object" are you referring to the GameObject or the Components ("scripts") attached to it?

    Activating and deactivating a GameObject necessarily enables and disables all of the attached Components as well.

    You can enable and disable components one at a time. Get a reference to the relevant component, and then set it's ".enabled" property to true or false. This will turn just that Component in the GameObject on and off, and do what @neginfinity said with calling OnEnable.
     
  4. AstridLongi

    AstridLongi

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

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    Why are you trying to track the odd/even button clicks count as that's what you're alreadyClicked modulus 2 not equal to zero line is doing?

    Code (CSharp):
    1. if (_AlreadyClicked%2!=0 ) {
    This line will be true when the value of _AlreadyClicked is odd.

    If you make it a clickCounter then you can just check to see if it's greater than zero?

    Note: You can use toggle buttons and toggle groups if you want different buttons to toggle different objects.

    Also when something is disabled then everything under that object is also disabled. So you need to ensure the objects you are toggling are seperate game objects. Ditto for any scripts you want to have running as they are only active when their parent gameObject is enabled.
     
    Last edited: Apr 21, 2020
    Ryiah likes this.
  7. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,519
    No, that's not a thing that happens. neginfinity already pointed you towards how you can run code when things are enabled and disabled.

    As for the question in your video, "is there a better way to reload the current level?", yes there is. SceneManager.LoadScene(...) is what you want for that. You have it in there but commented out, so what was wrong with it?

    I suggest asking questions in the scripting section of the forum. This specifically isn't a support area, and someone there might be happy to step you through this.
     
    Ryiah likes this.
  8. KaveeM2007

    KaveeM2007

    Joined:
    Jan 18, 2022
    Posts:
    21
    My solution is kind of silly, but it worked for me. I just turned off the gameObject which the changed object it attached, in my case it was a Canvas(UI). I did it by "Canvas.gameObject.setActive(false)". and then in the next frame I just made the gameObject active again. Like "Canvas.gameObject.setActive(true)" in my case.[ It does not also cause any visual problems in the game. ]
     
Thread Status:
Not open for further replies.