- Home /
FPS game: weapons
Hi, i am trying to male an FPS game, so i have a character and two weapons, but what should i do to change weapon ?? i need to change one object with another... What does it work ? Someone can explain me this or give me some examples ?? Thank you so much ;)
Answer by deltamish · Apr 15, 2014 at 03:24 PM
Hi, What you are trying to achieve is really darn simple all you nedd to have is some programming knowledge
Create a new empty gameobject and make your weapos as the child of the new object and attach the script to the object created //Note this script is in JavaScript but since i use lot of C# i amy have made few syntax problems without my knowledge
public var Weapon:GameObject;
function Update()
{
if(Input.GetKeyDown("1")) // Use keycode.Alpha1 instead used this tome it simpler to understand
{
SetActive(1);
}
if(Input.GetKeyDown("2")) // If you press 2 then select second object
{
SetActive(2);
}
}
function SetActive(WeaponInt:int)
{
for(i:int =0; i < transform.childCount;i++) // iterate through this Object's Children
{
if(i == WeaponInt) // if Selected int == current int that is i then Enable the object
transform.GetChild(i).gameObject.SetActive(true);
else // Disable it
transform.GetChild(i).gameObject.SetActive(false);
}
}
There is an error in the code, i am using C#... The errors is on the 25th row : Is not possible to use activeSelf...
hi Sorry about that i dunno why i did that mistaake , this is an javascript one ,are you converting it or using it directly
Try making it:
transform.GetChild(i).gameObject.SetActive(true);
Oww sorry, I came across this question and thought I'd comment right away. Anyways, glad it worked!
Answer by komodor · Apr 15, 2014 at 03:15 PM
you can just hide one and show other by code
or you can play hiding animation and then play showing other animation
So, i should put every weapons in the right position and then hide the weapons that i am not using ? rigth?
Your answer
![](https://koobas.hobune.stream/wayback/20220613142754im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make a weapon index? 1 Answer
Weapon Shake 2 Answers
Change level after destroying all enemies? 1 Answer
mesh? syntax question. 1 Answer
Any help with a weapon changing script? 2 Answers