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 Bluex · Dec 19, 2011 at 02:00 AM · errormouseweaponscrollswitching

Weapon Switch Script Help

Hey guys, Im working on a script to switch weapons. First off, I'm getting a Null Reference Exception error and I'm not sure why. Could you guys help me fix that and add what im trying to add into this script? I wanted to allow the player to switch weapons by scroll the mouse wheel up or down and I wasn't sure how to do that so I left it commented out.

 var counter : float = 0;

 function Start()
 {
     counter = 0;
     GameObject.Find("FPS_Weapon").SetActiveRecursively(true);
     
     GameObject.Find("Glowstick01").SetActiveRecursively(false);
     GameObject.Find("flashlight").SetActiveRecursively(false);
 
 }
 
 function Update () 
 {
 
 if (Input.GetKeyDown("1"))
 {
     counter =0;
 }
 
 if (Input.GetKeyDown("2"))
 {
     counter =1;
 }
 
 if (Input.GetKeyDown("3"))
 {
     counter =2;
 }
 
 //allows the player to change weapons by scrolling the mouse as well
 
 //mouse scroll up
 //counter = counter +1;
 
 //mouse scroll down
 //counter = counter -1;
 
 
 
 //prevent scrolling out of 'range'
 if (counter == 3)
 {
     counter = 0;
 }
 
 if (counter == -1)
 {
     counter = 2;
 }
 
 
 //changes weapons based on what the variable 'counter' is equal to
 if (counter == 0)
 {
     GameObject.Find("FPS_Weapon").SetActiveRecursively(true);
     
     GameObject.Find("Glowstick01").SetActiveRecursively(false);
     GameObject.Find("flashlight").SetActiveRecursively(false);
 
 }
 
 if (counter == 1)
 {
     GameObject.Find("Glowstick01").SetActiveRecursively(true);
     
     GameObject.Find("FPS_Weapon").SetActiveRecursively(false);
     GameObject.Find("flashlight").SetActiveRecursively(false);
 
 }
 
 if (counter == 2)
 {
     GameObject.Find("flashlight").SetActiveRecursively(true);
     
     GameObject.Find("FPS_Weapon").SetActiveRecursively(false);
     GameObject.Find("Glowstick01").SetActiveRecursively(false);
 
 }
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by syclamoth · Dec 19, 2011 at 02:16 AM

You should avoid using GameObject.Find whenever you can. Instead, keep inspector-assigned references to all the objects in question, which is faster and more reliable. As it is, without knowing exactly what line the NRE is occuring on (sscce.org guys!), I can only assume that it is happening because at least one of those GameObject.Find calls has a misspelled or non-existant target, and so returns null because the requested object doesn't exist.

In any case, a much nicer way to manage weapons would be to create some kind of 'weapon' class, and then use an array or list of them which can be selected with an int or enum. Each 'weapon' can have different behaviour and renderers, but can be treated the same by the swapper script.

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 Mr_Manza · Jul 01, 2014 at 04:33 PM 0
Share

hey when you mean weapon class, do you mean like separate objects? like for example my player objects? then write a script to control them with numeric values? (sorry I am a newbie and was curious about a system like this for a 2D game)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Weapon Switching 2 Answers

WeaponScript not working 0 Answers

Weapon selection system 2 Answers

Magic mouse scroll-wheel 1 Answer

How Does Weapon Selection Work In-Game? 0 Answers


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