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

[Solved] C# beginner - Simple "If then disable component" help needed

Discussion in 'Scripting' started by krousty_bat, Feb 8, 2016.

  1. krousty_bat

    krousty_bat

    Joined:
    Oct 9, 2012
    Posts:
    60
    Hey

    I'm really a beginner in C#, and I must say i get lost sometimes even after checking out the basic tutorials
    could you help me finish this simple "IF -> and tag is the right one -> disable Component of it"

    Code (CSharp):
    1. // create AE2D as an AreaEffector2D ?
    2. private AreaEffector2D AE2D;
    3.  
    4.         // if a "shield" is triggered...
    5.         if (shield)
    6.         {
    7.         // and if the contact is with the object with the Right Tag...
    8.          if (testContact.CompareTag ("rightTag"))
    9.             {
    10.                // Disable the Component "Area Effector 2D" of the object(s) with right Tag
    11.                // or changing the forceMagnitude to 0              
    12.                AE2D = gameObject.GetComponent<AreaEffector2D>();
    13.                AE2D.forceMagnitude= 0f;
    14.              }
    15.         }
    I hope i'm not too far from what needs to be done, and that my request is explained well enough.
    Thx.
     
    Last edited: Feb 8, 2016
  2. krousty_bat

    krousty_bat

    Joined:
    Oct 9, 2012
    Posts:
    60
    Well, I was close to it... the only line that I had to change was
    Code (CSharp):
    1.              AE2D = testContact.gameObject.GetComponent<AreaEffector2D>();
    I'm not sure I completely get why yet, but it seems to be it.
    Thx.
     
  3. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Use AE2D.enabled = false instead
     
    krousty_bat likes this.
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    "gameObject" on its own refers to the GameObject that the script is being run on. testContact.gameObject is the same, but for the testContact component.
     
    krousty_bat likes this.
  5. krousty_bat

    krousty_bat

    Joined:
    Oct 9, 2012
    Posts:
    60
    Thx a lot for your answers guys !