C
C#7mo ago
ULTIMA

how do i set a character controller as disabled or active in a c# code?

hey so quick question about the character controller, its very nice and easy but in my game i have traps that tp the player to a set location after taking damage, but it doesnt really work with character controller, is it because its using transform.position?
if (other.CompareTag("Player")) { player = other.GetComponent<HealthUiManager>(); if (player != null) { player.TakeDamage(damageAmount); player.transform.position = respawnPosition; } }
19 Replies
ULTIMA
ULTIMAOP7mo ago
please ping me when you respond so i dont forget to look
Buddy
Buddy7mo ago
You need to disable the character controller first, then set the position after that you can enable it again. It is a known thing when using CharacterController component Also, prefer to use TryGetComponent instead of GetComponent.
ULTIMA
ULTIMAOP7mo ago
i dont understand? disable the component character controller, then set the position, then enable it again?
Buddy
Buddy7mo ago
Correct
ULTIMA
ULTIMAOP7mo ago
alright ill try, thanks
Buddy
Buddy7mo ago
Regarding this. GetComponent allocates memory even if the result is null, however. Unity hates memory allocation, so prefer TryGetComponent as it doesn't allocate memory if the result is null You can use it like this
if (TryGetComponent(out HingeJoint hinge))
{
hinge.useSpring = false;
}
if (TryGetComponent(out HingeJoint hinge))
{
hinge.useSpring = false;
}
Or reversed
if (!TryGetComponent(out HingeJoint hinge))
{
Debug.Log("Could not find Hinge component");
return;
}
hinge.useSpring = false;
if (!TryGetComponent(out HingeJoint hinge))
{
Debug.Log("Could not find Hinge component");
return;
}
hinge.useSpring = false;
ULTIMA
ULTIMAOP7mo ago
ah ok, thanks for the tip when i try to use trygetcomponent it gives me an error, if (other.CompareTag("Player")) { player = other.TryGetComponent<HealthUiManager>(); if (player != null) { player.TakeDamage(damageAmount); player.transform.position = respawnPosition; } }
Buddy
Buddy7mo ago
My apologies, forgot to link to docs https://docs.unity3d.com/ScriptReference/Component.TryGetComponent.html TryGetComponent returns a boolean whether it succeeded or not. true / false it doesn't return the instance itself you need the out keyword, as seen in the example So in your case it would become
if (other.TryGetComponent(out HealthUiManager player))
{
player.TakeDamage(damageAmount);

player.transform.position = respawnPosition;
}
if (other.TryGetComponent(out HealthUiManager player))
{
player.TakeDamage(damageAmount);

player.transform.position = respawnPosition;
}
ULTIMA
ULTIMAOP7mo ago
yeah ok, thanks
Buddy
Buddy7mo ago
anytime.
ULTIMA
ULTIMAOP7mo ago
im very new to c# if you couldnt tell lol
Buddy
Buddy7mo ago
It's fine, we all have been new once. :^)
ULTIMA
ULTIMAOP7mo ago
how do i even disable a component in a C#? its not a setactive as far as i could tell, i tried getting the component then setting it as false but that didnt work @Buddy
Buddy
Buddy7mo ago
You can do this
private CharacterController controller;
void Awake()
{
controller = GetComponent<CharacterController>();
}
private CharacterController controller;
void Awake()
{
controller = GetComponent<CharacterController>();
}
ULTIMA
ULTIMAOP7mo ago
yeah im struggling with another problem now lol
Buddy
Buddy7mo ago
I see, with what? You don't need to create multiple help threads per question, if they are about the same subject (in this case Unity).
ULTIMA
ULTIMAOP7mo ago
i havent, but basically for my traps that i use , the code is in the trap, not the player, as i want variable damage amounts, so when i try to disable the character controller, it cant because its not able to get it. https://paste.mod.gg/mmvbehcbhszx/0
BlazeBin - mmvbehcbhszx
A tool for sharing your source code with the world!
Buddy
Buddy7mo ago
Also, please avoid cross-posting. IE posting both in help and in other channels As I mentioned, disable, teleport, enable.
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Trap"))
player.TakeDamage(damageAmount); // This line will only be executed if tag is "Trap"

// Everything after will always be executed.
controller.enabled = false;
// add TELEPORT code HERE
controller.enabled = true;
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Trap"))
player.TakeDamage(damageAmount); // This line will only be executed if tag is "Trap"

// Everything after will always be executed.
controller.enabled = false;
// add TELEPORT code HERE
controller.enabled = true;
}
Also, so you know, if you have no code block after an if-statement it will only work with a single line.
ULTIMA
ULTIMAOP7mo ago
i figured it all out, i had the voids i was trying to take over to another script as private
Want results from more Discord servers?
Add your server