Ok i have seen posts about this many times but they dont seem to help my situation this is what i have so far
public bool InCar;
public Transform DriverSeatLoc;
public GameObject Car;
public GameObject Player;
public Component Charactercontroller;
// Use this for initialization
void Start () {
Charactercontroller = GetComponent<CharacterController> ();
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.E)) {
CharacterController.enabled = false;
Player.transform.SetParent(Car);
}
}
}
i am using the unity fps character and im trying to disable the attached script called “Character Contoller” but when i use the script CharacterController.enabled = false; it says that it doesnt know what enabled is i have also tried lower case c on controller and i have tried enable instead of enabled i have no idea why this will not work if any one could please help me that would be great
P.S. Im trying to make this game multiplayer but i have no idea how to make this only work with the character that is clicking near the car i have a fear that it will just make everyone attached to the car i am using photon networking thanks again.
As I’ve never even tried something like this (using “Component” instead of the name of a specific component type- in this case the script name), I have no idea if it works, but I’d assume even if it did it would be involving casting a specific component to a generic component. What you need to define is:
public CharacterController Charactercontroller;
then initialize it in Start() exactly the same as it is now, and make sure that
Charactercontroller.enabled = false;
is the name of the reference and NOT the name of the class.
ok thank yyou that worked but now i have a problem with my script i am using photon for my multiplayer and im watching a video on it but its for unity 5 and this is the code that is having problems
it stops at Going to Enable Stuff and wont find the CharacterController component idk if you can help me with this either but if you can that would be awsome thanks.
P.S. im watching this video
MonoBehaviour is the base class for components and casting to it probably isn’t going to work at all (much less how you want it to).
If you use GetComponent, you can skip the casting necessity by using GetComponent() instead of GetComponent(“ScriptName”). See the difference? If you DID want to continue using the string method, you cast a CharacterController script (for instance) to CharacterController, not to Component or MonoBehaviour.
Not necessary, I was simply giving an example of the kind of mistake that was occurring in the script. Don’t bother assigning the controllers to a variable unless you need the reference later- if you’re only going to use it once (like in your script), then just keep in mind what I said about how/when to cast GetComponent and use that to write out the “enable” lines.
ok that works for the CharacterController but when i put the FirstPersonController in the script it is highlighted red and the .enabled is red as well the FirstPersonController is a script thank you for the quick replies you have been extremly hepful.
If it’s highlighted red, then there’s no class by that name in the current namespace. If you’re absolutely sure that there’s a script/class with that exact name and exact capitalization, then it’s probably not in the same namespace. Do a little search on namespaces to figure out how they work and what they’re used for, then check the FirstPersonController script and see which one it belongs to (you’ll be able to tell because the entire class will be surrounded by “namespace SomeNamespace{ }”. Either include that namespace at the top of your own script like so:
using UnityStandardAssets.Character;
or each time you need to reference the class within that script, do so with its full name (including namespace) like so
Keep in mind that this may not be the actual namespace the script belongs to as I don’t know which script you’re trying to use- you’ll need to check which one it uses on your own.