How to ACTIVATE and DEACTIVATE COMPONENTS through script in Unity

Поділитися
Вставка
  • Опубліковано 16 лис 2024

КОМЕНТАРІ • 23

  • @GameDevTraum
    @GameDevTraum  2 роки тому

    I have another channel, please SUBSCRIBE here 😄👉 www.youtube.com/@GameDevTraumDE
    *LEARN more UNITY here* 👉 ua-cam.com/video/E8-k9_DdkBg/v-deo.html

  • @Brai-niac
    @Brai-niac Рік тому +1

    very simple and easy to do. Welldone ✌✌

  • @ohN0thing
    @ohN0thing Місяць тому

    Hey I know this video is a bit old, but Is it possible to re-activate the object on the 2nd button press

  • @micheleguardabasso8162
    @micheleguardabasso8162 Місяць тому

    as i can doing this with game object cross trigger and deactivate componet of this game object exsample audio sorcue script?

  • @Arisawa_Heavy_Ind
    @Arisawa_Heavy_Ind 3 роки тому +1

    Thank you

  • @cookingwithtommy
    @cookingwithtommy 3 роки тому +2

    do you do the same for a rigid body 2d? If not please tell me how to

    • @nicholaspitti8171
      @nicholaspitti8171 3 роки тому +1

      According to one forum thread "myRigidbody.simulated = false;" or "myRigidbody.isKinematic = true; " will be the alternative to disabling rigidBody.
      You cant use enable or SetActive() property on all components(for example RigidBody or ParticleSystemForceField). But you can click the question mark icon in the unity inspector and it'll take you to the documentation page. Click on the switch to scripting button to see which properties the component has.
      Note: My first comment was marked as spam cause it had a link to a unity forum. Your going to have to google How to disable Rigidbody and it should be the one that says [SOLVED].

  • @martinsaviationlife2690
    @martinsaviationlife2690 2 роки тому +1

    I accidently added a copy of the script as a component, how do I DELETE it?
    Also this sounds like an ASMR video

    • @GameDevTraum
      @GameDevTraum  2 роки тому +1

      In the up right corner of the component there are three dots, click there and then remove

    • @martinsaviationlife2690
      @martinsaviationlife2690 2 роки тому

      @@GameDevTraum thankyou, for the quick response

    • @GameDevTraum
      @GameDevTraum  2 роки тому

      you're welcome! if you want more asmr videos in english I'm currently uploading new ones in this other channel xD
      ua-cam.com/channels/sRsHAtbb07j_e1k25e_8Cw.htmlvideos

  • @lifeguardval14
    @lifeguardval14 2 роки тому

    how can i turn off a nav mesh agent?

  • @suleiman8610
    @suleiman8610 3 роки тому

    I have an Image in my canvas that I want to deactivate how could I deactivate the Image without deactivating the whole canvas

  • @dwiNExx
    @dwiNExx 3 роки тому +1

    How about enabling or disabling a postproces effect component with a press of a button

    • @GameDevTraum
      @GameDevTraum  3 роки тому

      Ahh that's interesting! Let me work on it. I'll answer to this comment

    • @dwiNExx
      @dwiNExx 3 роки тому

      Thanks :)

    • @GameDevTraum
      @GameDevTraum  3 роки тому

      heey! the videos and articles about this are gonna take some time, I don't want to keep you wating, I'll solved the problem and create a unity package that you can download and import into your project, here it is:
      gamedevtraum.com/en/sdm_downloads/post-proccessing-study/
      Inside the package are several comments that explain how it's done, basically you assign the script to any GameObject, then drag the Post Proccess Volume in the field in the inspector of this GameObject.
      This solution is solved activate and deactivate the "Color Grading" effect, if you want to use it for other effect you simple define another type variable, for example:
      Bloom bloomEffect;
      then get the reference from the profile (in Start there is an example).
      Then you can enable and disable bloom or access it's parameters through code.
      Hope you find it useful!
      btw I'll be uploading new videos in english in this other channel:
      ua-cam.com/channels/sRsHAtbb07j_e1k25e_8Cw.html

    • @dwiNExx
      @dwiNExx 3 роки тому

      @@GameDevTraum Thanks
      Very much appreciated

    • @dwiNExx
      @dwiNExx 3 роки тому

      @@GameDevTraum how about for Chromatic Aberration?

  • @mr_mcjosh1850
    @mr_mcjosh1850 3 роки тому

    How would i make it turn off a game component onclick instead of a keyboard key?

    • @GameDevTraum
      @GameDevTraum  3 роки тому +2

      mmm the instruction for turn off a gameobject is the same, but you have to find a way to execute it exactly when you need it. I'm not sure what you wanna do, but lets suppose you want to click on the screen where a gameobject is drawed and you want that gameobject to disappear. So first you need to detect a left click event, that click will occur on the screen, that is to say the position is expressed as a Vector2, so in order to know if you are clicking a gameobject from the scene you have to trace a ray from that screen point to the world. You can analyze the collider the ray touches (if there si one), so if you can access the collider of a gameobject you can access the gameobject itself, read its tag or see if it has a certain component assign (or whatever you need in order to determine if that object can be turned off). If the object can be turned off then you execute the instruction SetActive from this video on that gameobject.
      So to achive this you'll need to investigate some things, here is a link:
      docs.unity3d.com/ScriptReference/Camera.ScreenPointToRay.html
      I'll leve you a starting point:
      ////instructions
      public Camera camera;//assign your render camera in the inspector
      Ray ray;
      RaycastHit hitObject;
      void Update(){
      if(Input.GetMouseButtonDown(0)){
      LeftClick();
      }
      }
      void LeftClick(){
      Vector3 mousePositionOnTheScreen = Input.mousePosition;
      ray = cam.ScreenPointToRay(mousePositionOnTheScreen);
      if(Physics.Raycast(ray, out hitObject)){
      //here the ray touches something
      hitObject.collider.gameObject.SetActive(false); //disable the object that the ray touched
      }
      }
      ///
      Hope you find it useful

  • @felixalcatraz7489
    @felixalcatraz7489 Місяць тому +1

    T_T i'm here for the rigid body one and he delibretly skiped over it

    • @GameDevTraum
      @GameDevTraum  Місяць тому

      hahha yeah sorry about that, rigidbody doesn't have an "enable" checkbox so it's not that simple to disable it.
      I'm not sure is a appropriate way to do it, but you could change the "isKinematic" parameter from the rigidbody to true, the "detectCollisions" parameter from the rigidbody to false and also the "useGravity" variable to false, that should stop the evaluation of physics in the rigidbody, I would have to test it to be sure. Then, when you need to enable the rigidbody, you revert those parameters, "isKinematic" to false, "detectCollisions" to true and "useGravity" to true.
      To do the thing before you need to define in your script a Rigidbody type variable, for example let's call it "myRigidbody", should be something like this:
      public Rigidbody myRigidbody;
      Then initialize that variable with the Rigidbody component you want to use, to do this one thing you can do is to drag the GameObject with the Rigidbody component in the variable in the inspector.
      Then if you want to change the isKinematic parameter you use the variable and the dot operator:
      myRigidbody.isKinematic = true;
      myRigidbody.detectCollisions = false;
      myRigidbody.useGravity=false;
      If that doesn't work, another alternative is to save the velocity and angular velocity of the rigidbody and then destroy it, that would be the same as deleting the component from the inspector, the result is that your object won't move anymore due to the rigidbody because the component is no longer there affecting the object. Then, when you need to "enable" it you could add the Rigidbody component by script and apply the velocity and angular velocity you previously saved.