- Home /
switch weapons script not working?
my switch weaopns script isnt working it keeps giving me this error please help: Prefab GameObject's can not be made active! (secondary) UnityEngine.GameObject:set_active(Boolean) SwitchWeaponsScript:Main() (at Assets/GunScripts/SwitchWeaponsScript.js:9)
and here is my script: `var primary : GameObject; var secondary : GameObject;
if(Input.GetKeyDown("1")); primary.active = true; secondary.active = false;
if(Input.GetKeyDown("2")); secondary.active = true; primary.active = false;
`
still not working put the weapons in the hierarchy an it still wont switch weapons please help its still the same script just the guns are in the hierarchy
Answer by Shrandis · Oct 03, 2012 at 09:49 AM
Your problem is that you're trying to activate/deactivate a prefab, instead of an instance of that prefab.
var primary : GameObject
If I'm right, you've assigned your primary weapon prefab to this variable from Inspector. As I've said in my first sentence, activating/deactivating prefabs isn't what you want to do.
Assign your primary/secondary weapon object which is in the scene hierarchy to your variables, NOT primary/secondary weapon objects in the Project folder.
If he's doing it all in-script, then
var primary: WeaponPrefab;
var secondary: WeaponPrefab;
primary = new WeaponPrefab;
secondary = new WeaponPrefab();
should fix it, given that WeaponPrefab() is a class; If it's a prefab, you should use instantiate.
http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html
to set 'primary' and 'secondary' as clone of your prefab
I do not understand why you assumed his weapon prefab to be a class, I can't find any hint to that in his question.
Also, since the OP only mentioned that he has trouble SWITCHING weapons, I assumed the weapon gameobjects are already in the scene-view.
I was offering it as a different workaround and nothing else.
To answer his question again but in a way that you may find more pleasant : he needs to either instantiate the prefab by hand or in-script and THEN attach it to his 'primary'/'secondary' vars.
///// "Given that WeaponPrefab() is a class" is a logic based assertion.
example: You would use x to solve this and if you do then you would also have to assume y is WeaponPrefab() (a.k.a. use y that is a WeaponPrefab()) but that's just irrelevant.
("I do not understand why you assumed his weapon prefab to be a class." this sentence hurts my brainmuscle)
"To answer his question again but in a way that you may find more pleasant : he needs to either instantiate the prefab by hand or in-script and THEN attach it to his 'primary'/'secondary' vars."
This is the summary of my answer + your comment. I told how it should be done if he instantiated his prefabs in the scene, you told how if he instantiated his prefabs in runtime.
"("I do not understand why you assumed his weapon prefab to be a class." this sentence hurts my brainmuscle)" good job missing the point :P
Let's hope this wall of text doesn't cause unnecessary confusion :)
Shrandis's answer is right and dotjack is just reaching, in case anyone got confused with their back-and-forth.
Answer by nikescar · Oct 03, 2012 at 09:13 AM
Your if statements should look like this:
if(Input.GetKeyDown("1")){
primary.active = true;
secondary.active = false;
}
if(Input.GetKeyDown("2")){
secondary.active = true;
primary.active = false;
}
It is true that his statements are wrong. (wrong as in "won't do what he wanted to do") but that won't cause an exception.
Your answer
Follow this Question
Related Questions
FPS one hands/arms model with multiple weapons/guns models 1 Answer
What's wrong with my weapon switching/swapping script? 0 Answers
FPS Weapon inventory script help 0 Answers
FPS - Gun Delay while Rotating 0 Answers
My gun wont fire ! 1 Answer