Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by FrostHDD · Oct 02, 2012 at 09:23 PM · fpsgunswitchweapons

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;

`

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FrostHDD · Oct 21, 2012 at 03:29 PM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dotJack · Oct 03, 2012 at 10:18 AM 0
Share

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

avatar image Shrandis · Oct 03, 2012 at 10:39 AM 0
Share

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.

avatar image dotJack · Oct 03, 2012 at 11:42 AM 0
Share

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)

avatar image Shrandis · Oct 03, 2012 at 11:52 AM 0
Share

"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 :)

avatar image Dreamblur · Oct 03, 2012 at 11:55 AM 1
Share

Shrandis's answer is right and dotjack is just reaching, in case anyone got confused with their back-and-forth.

Show more comments
avatar image
0

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;
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Shrandis · Oct 03, 2012 at 09:45 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges