Hi everyone
I’m very new to scripting, so just pretend I’m a 5 years old child…
I need to teleport my FPS Controller, I tried many ways, and as a lot of people when I enter my teleport trigger or press a teleport key I can see just for a frame the new position location but it doesn’t last and my FPS Controller didn’t move.
So I understood a solution would be to disable the FPS Controller script before teleporting and re-enable it after. But when I do this :
You need a reference to the instance of the CharacterController component instead of trying to enable/disable the CharacterController class itself. You’d typically get this reference with a reference set in the inspector or with GetComponent.
Setting a reference in the inspector, you’d add something like the following to your script. Then drag the instance of the CharacterController script attached to the object to the new field you will see in the inspector. Then everywhere you try to enable/disable the CharacterController you do it against MyController instead of CharacterController.
public CharacterController MyController;
Doing it with GetComponent is common too, but has a slight performance hit compared to the inspector reference. But sometimes it is the best way depending on the situation. It is normally best to cache such a reference so you don’t need to GetComponent again every time you need it again, so code often looks something like this:
private CharacterController myController = null;
void Start()
{
myController = gameObject.GetComponent<CharacterController>();
if (myController == null) //if GetComponent fails it returns null, so it is good practice to check for that so you aren't surprised when you later get null reference runtime errors in your log
{
Debug.Log("CharacterController component could not be found");
}
}
Then you’d similarly use myController for the rest of your script where you’re currently using CharacterController. Look up GetComponent for more information.
Note that I have no idea if enabling/disabling your CharacterController component will have any benefit for your actual teleportation issue. I’m just trying to help with those compile errors in your code.
To expand a little bit on what Uncle Joe said there, when you use the formal name of a class such as CharacterController.whatever, you are trying to access a static property called “whatever” instead of an instance property. Any static property is shared across all instances of a class, and in this case that isn’t how .enabled property works. It is an instance property, which means that EVERY instance of that class has its own .enabled.
So make a spot in your brain to identify the difference between something that is a static property and something that is an instance property, because it will enable you to reason more accurately about what you’re wrestling with.
Yes! Thank you so much both of you!
You’ve been a great help and it worked!
For the record I’m a composer and I’m currently studying sound design and integration in unity with wwise.
So scripting is not and won’t be my job, but I have a “walking sound simultaor” to produce for my studies in unity from scratch. And I 'm discovering this programming world and it’s very interesting with a thoughtfull and nice community.
So thank you so much.
If by any chance, and since you seem to know what you’re doing…, I have another thread with an other issue open. If you have 5 minutes to lose please have a look. If not don’t worry, I’m very grateful for the time you took to answer me.