- Home /
Activating and deactivating problem
I am activating and deactivating my guns on collision to create a weapon pick up feature. For example the Player starts with no gun because they are all deactivated on start, but if he collides with an object with a tag of say UZI that gun will become active, keeping all the other guns deactivated. This doesnt work how I would of thought, instead it only activates the M14 keeping everything else deactivated no matter which tagged object I collide with. I hope you understand my script better than my explanation, hope someone can help.
var glock : GameObject;
var UZI : GameObject;
var G36C : GameObject;
var M14 : GameObject;
function Start () {
UZI.active = false;
glock.active = false;
G36C.active = false;
M14.active = false;
}
function OnCollisionEnter(collision: Collision) {
if (collision.gameObject.tag == "UZI");
UZI.active = true;
glock.active = false;
G36C.active = false;
M14.active = false;
if (collision.gameObject.tag == "glock");
UZI.active = false;
glock.active = true;
G36C.active = false;
M14.active = false;
if (collision.gameObject.tag == "G36C");
UZI.active = false;
glock.active = false;
G36C.active = true;
M14.active = false;
if (collision.gameObject.tag == "M14");
UZI.active = false;
glock.active = false;
G36C.active = false;
M14.active = true;
}
Answer by whebert · Mar 26, 2013 at 02:50 PM
You need to surround each one of your if statement logic with { }, and not put a semicolon after the if. Like so:
if (collision.gameObject.tag == "UZI")
{
UZI.active = true;
glock.active = false;
G36C.active = false;
M14.active = false;
}
Answer by ZorNiFieD · Mar 26, 2013 at 02:54 PM
do what whebert said, but also do this:
UZI.SetActive(true); // correctly sets object and all subobjects active recursively
UZI.active = true; // not correct
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Gun aim off center 0 Answers
Gun swap through collision 1 Answer
Goes faster when moving to monster's transform 1 Answer
damage cooldown 1 Answer