- Home /
Entering/Exiting Vehicles Part 2
I fixed some of the problems and
if (Input.GetButtonDown("Vehicle")&& isPlayerVisible)
{
Debug.Log("Driving");
player.gameObject.SetActiveRecursively(false);
player.gameObject.active = false;
player.parent = exitpoint.transform;
player.transform.localPosition = Vector3(-1.5f, 0, 0);
exitpoint.parent = car.transform;
exitpoint.transform.localPosition = Vector3(-1.5f, 0, 0);
GameObject.Find("FireTruck").GetComponent<FireTruck>().enabled=true;
PlayerCamera.enabled = false;
CarCamera.enabled = true;
}
else
{
if (Input.GetButtonDown("Vehicle"))
{
Debug.Log("Driving");
player.gameObject.SetActiveRecursively(true);
player.gameObject.active = true;
player.transform.parent = null;
exitpoint.parent = car.transform;
GameObject.Find("FireTruck").GetComponent<FireTruck>().enabled=false;
PlayerCamera.enabled = true;
CarCamera.enabled = false;
}
}
There's another error, EnterExitVehicle.FireTruck' is a field' but its used like a `type' and UnityEngine.Vector3 is a 'type' but used like a variable. How can I fix this?
Can we see the whole script? That way, we can see whats wrong. Also, the concept you are using for assigning values like Vector3(-1.5f, 0, 0)
is used in C++ but not in C#. You will have to use new Vector3(-1.5f, 0, 0)
ins$$anonymous$$d.
Adding on, from the error it seems that maybe you have defined a field in your class named FireTruck and not a class. In GetComponent() you have to enter the class name which might be something else.
sure
Transform FireTruck;
Transform player;
Transform exitpoint;
Transform doorTriggerLeft;
Camera PlayerCamera;
Camera CarCamera;
bool isPlayerVisible;
void Start (){
}
void Update (){
if (Input.GetButtonDown("Vehicle")&& isPlayerVisible)
{
Debug.Log("Driving");
player.gameObject.SetActiveRecursively(false);
player.gameObject.active = false;
player.parent = exitpoint.transform;
player.transform.localPosition = Vector3(-1.5f, 0, 0);
exitpoint.parent = FireTruck.transform;
exitpoint.transform.localPosition = Vector3(-1.5f, 0, 0);
GameObject.Find("FireTruck").GetComponent<FireTruck>().enabled=true;
PlayerCamera.enabled = false;
CarCamera.enabled = true;
}
else
{
if (Input.GetButtonDown("Vehicle"))
{
Debug.Log("Driving");
player.gameObject.SetActiveRecursively(true);
player.gameObject.active = true;
player.transform.parent = null;
exitpoint.parent = FireTruck.transform;
GameObject.Find("FireTruck").GetComponent<FireTruck>().enabled=false;
PlayerCamera.enabled = true;
CarCamera.enabled = false;
}
}
}
void OnTriggerEnter ( Collider Player ){
Debug.Log("Trigger Enter, Player Detected");
isPlayerVisible = true;
}
void OnTriggerExit ( Collider Player ){
Debug.Log("Trigger Exit");
isPlayerVisible = false;
}
Answer by Kutagh · Jul 16, 2014 at 04:37 PM
You're doing 'Vector3(x, y, z)' which is not valid, since Vector3 is a type and not a function. You want to call the constructor of the Vector3 type by doing 'new Vector3(x, y, z)' to create a new Vector3 with the specified parameters.
As for FireTruck: 'GetComponent()' needs the type of the component you want. Are you sure that you have a component called FireTruck? (remember that components are different from game objects, a gameobject is a collection of components). I cannot tell you how to fix this since it depends on the actual configuration of the GameObject and the specific components that you want to manipulate.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Entering/Exiting Vehicles 1 Answer
Distribute terrain in zones 3 Answers
Error CS0111 1 Answer
Both static and non-static method is giving me an error 2 Answers