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

Problem add/remove a component in C# by pressing a GUI.Button

Discussion in 'Scripting' started by Exnihilon, Sep 3, 2014.

  1. Exnihilon

    Exnihilon

    Joined:
    Mar 2, 2014
    Posts:
    157
    Hello to all. I'm having a hard time trying to add and remove a js script as a component to a gameObject in C#, as I'm novice to Unity and these coding.

    I have a js that needs to be added in MainCamera in runtime, by click of a GUI.Button. This script has to be destroyed later with the same GUI.Button clicked.

    I have managed to add the js script on the MainCamera in runtime with the following code snippet:

    Code (CSharp):
    1.  
    2. public Bearing addBearing; // "Bearing" is the js script and "addBearing the variable to reference this
    3.  
    4. //some other codding
    5. .
    6. .
    7. // Add and remove Bearing with this button
    8. public bool addBearing;
    9.  
    10. void OnGUI(){
    11. if(GUI.Button(new Rect(10,10, 100, 100), "Bearing"))
    12.  
    13. addBearing= !addBearing;
    14.  
    15. if(addBearing){
    16. addBearing  = GameObject.FindGameObjectWithTag("MainCamera").gameObject.AddComponent<Bearing>();
    17.  
    Problem is, when I click back the "Bearing" GUI.Button, the js script "Bearing" is not get destroyed as it should with this code snippet:

    Code (CSharp):
    1.  
    2. }else{
    3.  
    4. GameObject.FindGameObjectWithTag("MainCamera").gameObject.GetComponent<Bearing>();
    5.  
    6. Destroy(GetComponent<Bearing>());
    I know there is something missing in this code logic, but I'm not that experienced in coding C# to find a solution. So, can someone be kind enough to get me a helping hand with this code? It would be very much appreciated. Thank you all for your answers.
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Line 12 makes no sense.
    Bearing is not Boolean

    Also, you need to reference the component when you find it for destroying.
     
  3. Exnihilon

    Exnihilon

    Joined:
    Mar 2, 2014
    Posts:
    157
    Hello @hpjohn. Thank you for your reply to my question, but can you elaborate more, on how to reference the component the right way, cause I'm not very much familiar to C# and I'm having a really hard time to make this function work out.
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Okay i should rephrase a little
    you have 2 variables with the same name, which is bad (change the bool to eg. isAdded)
    The line where you find it when adding component, you assign the reference already, so you shouldn't need to find again, then you can destry it by just doing
    Destroy(addedComponent)
     
  5. Exnihilon

    Exnihilon

    Joined:
    Mar 2, 2014
    Posts:
    157
    So, in my posted code snippet should I only use, and omit the "FindGameObjectWithTag"?

    1. Destroy(GetComponent<Bearing>());
    I get no error in compiling and I get not the component destroyed at play. Can you give me a working example based on my code posting? It would be very nice of you, if you could help me a little more.
     
  6. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    Code (CSharp):
    1. public Bearing addBearing; // "Bearing" is the js script and "addBearing the variable to reference this
    2. //some other codding
    3. .
    4. .
    5. // Add and remove Bearing with this button
    6. public bool addBearingBool;
    7. void OnGUI(){
    8. if(GUI.Button(new Rect(10,10, 100, 100), "Bearing"))
    9. addBearingBool= !addBearingBool;
    10. if(addBearingBool){
    11. addBearing  = GameObject.FindGameObjectWithTag("MainCamera").gameObject.AddComponent<Bearing>();
    12. }else{
    13. Destroy(addBearing);
    14. }
     
    Last edited: Sep 3, 2014
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    That won't work for the reason already outlined - two variables with the same name.

    Ultimately - there's no reason to have a boolean at all. Just check if addBearing is null. If it is, call AddComponent. If it isn't then Destroy it and set addBearing to null.
     
    image28 likes this.
  8. Exnihilon

    Exnihilon

    Joined:
    Mar 2, 2014
    Posts:
    157
    Hello @KelsoMRK. Yes, setting to null the addBearing would be a solution. Problem is not the boolean used, but how to reference the destroy to the component, as it seem as the compiler cannot access the added script "Bearing", in order to destroy it. Need to mention that "Bearing" is attached to "MainCamera", but the add/remove script is running from another gameObject. Any clues?
     
  9. SkillBased

    SkillBased

    Joined:
    Aug 11, 2014
    Posts:
    141
    Forgive me if I didn't stare at your post to try to understand it in all its nuance but I thought I would pass along something I use that may work in your instance. This enables/disables the component rather than destroying/creating:

    ComponentType theComponent;

    theComponent.GetComponent<ComponentType>();

    theComponent.enabled = true;
    //or false
     
  10. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    You reference it via the variable you assign the AddComponent call to...
    Code (csharp):
    1.  
    2. Destroy(addBearing);
    3.  
    Edit - thinking about it more, there may be some cleverness built into Destroy that looks on the same object so it might not work when called from a different object. You might have to do a Find().Destroy(GetComponent<>)
     
    Exnihilon likes this.
  11. Exnihilon

    Exnihilon

    Joined:
    Mar 2, 2014
    Posts:
    157
    I believe you are right about it. Having tried to attach the "Bearing" script to the same GameObject, the Destroy component worked like a charm. I need thought to have it called from my other GameObject.