Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Sl0thzy · Oct 26, 2015 at 05:08 PM · c#boolshopweapon system

Changing a bool on only 1 instance of a prefab instead of all?

So I am creating this little weapon shop that just spawns a prefab'd gun when you buy it, which then you can go up to it and equip it. The problem I am having is I have it so that way the weapon only shoots if you have it in your hand, which is controlled by a bool. The issue I have is when there are 2 instances of one weapon in the scene, which, when I equip one gun, it changes the bool for both, making it so the weapon can't shoot. I was wondering what I would have to do to specify that only the bool for the specific instance is to be changed?

Comment
Add comment · Show 2
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 Jessespike · Oct 26, 2015 at 05:15 PM 0
Share

What does the script look like? Are you using statics? Player should have 1 reference to the weapon. ie: Player.EquippedWeapon, Accessing this reference will ignore the extra instances in the scene.

avatar image rageingnonsense · Oct 26, 2015 at 05:51 PM 0
Share

You need to post some code. It is not possible to help you without speculation otherwise. $$anonymous$$y first instinct though is that you are using a static property, when you should not be.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by starikcetin · Oct 26, 2015 at 05:34 PM

I imagine you use GameObject.FindObjectsWithTag which gives you an array of all objects with that tag. You need to target the gameObject directly (which means "Instance"). You can keep a variable of instantiated gameObject like: GameObject go = Instantialte(...) as GameObject;

If not, then i think your script has static variables and attached to prefab. Static methods/variables/fields etc. belongs to class itself, not the instance of object. So when you do something with a static method/variable/field, then you do that for all the instances of objects that has the class. Try using local fields and methods, then call them from scripts like:

 GameObject go = (target gameObject)
 go.GetComponent<TypeOfScript>().MethodName();
 
 go.Getcomponent<TypeOfScript>().field = newValue;
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 Sl0thzy · Oct 26, 2015 at 05:54 PM 0
Share

Yeah the bool is static variable as it is changed through another script. Looks like I will have to create a work around. Thanks!

avatar image
1

Answer by fffMalzbier · Oct 26, 2015 at 05:09 PM

Sounds like you are trying to manage 2 guns from one script instance.

The script that controls the gun should be on the gun and should not use static variables and function for controlling the gun. That way if you have 2 instances of the gun you will have a script instance for every gun object.

Comment
Add comment · 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

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

6 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Unity event calling function gets nullreferencexception on bool 0 Answers

Why is my variable always true? 1 Answer

Renderer on object disabled after level reload 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