C# Enabling and Disabling Components in Unity! - Beginner Scripting Tutorial

preview_player
Показать описание
Watch this video in context on Unity Learn:

How to enable and disable components via script during runtime.
Рекомендации по теме
Комментарии
Автор

Thank you Unity this helped me make a jump feature in one of my games I'm working on, Thanks!
Because I used the rigidbody component not the component called Light

_jexool_GD_
Автор

wow that was really cool a half hour of work and i can turn on a light

notbotpot
Автор

myLight.enabled = !myLight.enabled - that's reayl cool))

АлександрТитов-щв
Автор

Anybody else getting this error message

Assets\animatoractivator.cs(22, 24): error CS1061: 'Animator' does not contain a definition for 'enable' and no accessible extension method 'enable' accepting a first argument of type 'Animator' could be found (are you missing a using directive or an assembly reference?)

By the way I'm trying to activate an animation in a 2D game, so that might have something to do with it.

JoeAC
Автор

How can i make it to enable any component? Right now it says component does not contain definition of enabled

Kopellis
Автор

i have a joint i want to enable/ disable but that doesnt work with the the enable or disable

milan
Автор

Light is a specific part of the component what if I want to uncheck a box as a whole?

ismailhamed
Автор

But apparently not rigid body components 😢

ContinuedBread
Автор

How do I do this Rigidbody 2D components?

RockYeahh
Автор

how do i make it so it permanantly does myLight once its triggered

i tried this for example:
myLight.enabled = true;

danielcraft
Автор

How i disable a script, i mean, i need to change the light to script, i need to writw the name of the script somewhere in the code, please if anyone knows the answer tell me, i was looking for this like for 1 hour

franciscoleongomez
Автор

so whats the difference between using the "enabled" boolean and the "SetActive" function?

JonahDominguez
Автор

how to do it with collider with a player?

teraspa
Автор

I was just about to click off at 30 seconds but I am glad I didn't because I learned about the toggle trick

andreisava
Автор

how do you make it function for 2D light

Kevkf
Автор

bru I thought "enabled" is get only xD

brockdish
Автор

I have the code right i swear i checked for the 5th time but it wont work my light is still on heres my code

using UnityEngine;
using System.Collections;

public class HideAndShow : MonoBehaviour
{
private Light flashlight;

void Start()
{
flashlight = GetComponent<Light>();
}

void Update()
{
if
{
flashlight.enabled = !flashlight.enabled;
}

}
}

HMYT-jwto
Автор

it so short why and I want video so long to learn more

dragon
Автор

How do I add distance to the command in a First person 3D game? I Plan to set F as action and have that for opening and closing doors/drawers. When I enter this script, no matter where I am on the level, The light turns on and off. How do I set this to trigger as my character gets within a certain radius of my light?

Here is my script so far...

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnableComponents : MonoBehaviour
{
private Light myLight;
public float MaxDistance = 5;
public Transform PlayerCamera;


// Start is called before the first frame update
void Start()
{
myLight = GetComponent<Light>();
}

// Update is called once per frame
void Update()
{
if (Input.GetKeyUp(KeyCode.F))
{
myLight.enabled = !myLight.enabled;
}
}
}

scotttowers
Автор

how do i do it with an object instead of a light?

seanderiko