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; } }
if (other.CompareTag("Player")) { player = other.GetComponent<HealthUiManager>(); if (player != null) { player.TakeDamage(damageAmount); player.transform.position = respawnPosition; } }
19 Replies
please ping me when you respond so i dont forget to look
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.
i dont understand?
disable the component character controller, then set the position, then enable it again?
Correct
alright ill try, thanks
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
Or reversed
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;
}
}
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
yeah ok, thanks
anytime.
im very new to c# if you couldnt tell lol
It's fine, we all have been new once. :^)
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
You can do this
yeah im struggling with another problem now lol
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).
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!
Also, please avoid cross-posting. IE posting both in help and in other channels
As I mentioned, disable, teleport, enable.
Also, so you know, if you have no code block after an if-statement it will only work with a single line.
i figured it all out, i had the voids i was trying to take over to another script as private