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 brKuro · Nov 17, 2017 at 03:30 PM · scripting problemscript.scripting beginner

How to use GetComponent to modify variables of multiple gameobjects with the same script

I have a gameobject with a script that will control the variables of other gameobjects, how do I modify the variables of the other gameobjects individually? All gameobjects I'm going to modify the variables with are the same script.

I use translator, sorry.

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

2 Replies

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

Answer by Zodiarc · Nov 17, 2017 at 03:44 PM

You either assign all the game objects to a list within the script which controls them or you assign a tag to those objects and use

FindGameObjectsWithTag()

in the control 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 brKuro · Nov 17, 2017 at 05:27 PM 0
Share

Helped me a lot, now I can identify each object with the tags. Thank you very much.

avatar image
-2

Answer by shadowpuppet · Nov 17, 2017 at 03:57 PM

in C# you need to make the variables you want to modify public static . Such as if i have a script called ammunition and another script on my gun so that when I shoot it I want to deplete the bullets in the ammunition script. I would have bullets be : public static int bullets;

 //ammo script
 using UnityEngine;
 using System.Collections;
 
 public class ammunition: MonoBehaviour {
 public static int bullets = 10;
 public int bulletsLeft;//I do this because as a public static you can't see it in the inspector.you don't need this and can delete when you see it's working.but for now I have in my update function bulletsLeft = bullets so I can see that they are indeed going down because the bulletsLeft will show in the inspector an be the same value as bullets
 void Update(){
 
 bulletsLeft = bullets;
 }
 }
 
 //then in my gun script_____________________________________________________
 
 using UnityEngine;
 using System.Collections;
 
 public class gun: MonoBehaviour {
 MonoBehaviour bullets;// this is the static int variable I want to change
 public GameObject ammo; //this is the gameobject with the ammunition script on it;
 
 void Start(){
 bullets = ammo.GetComponent<ammunitiion>(); //the variable I want to change = gameobject with script<the script itself>();
 }
 void Update(){
 if (Input.GetKeyDown (KeyCode.Keypad0)) {
 ammunition.bullets -=1;// so every time I press keypad 0 the bullet int on my ammunition script goes down by 1
 }
 }
Comment
Add comment · Show 2 · 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 Hellium · Nov 17, 2017 at 04:49 PM 0
Share

in C# you need to make the variables you want to modify public static.

No you don't. Do you know what implies the static keyword when declaring a class member? It means that the value of this class member will be shared by every instances of the class. Supposing you have two wepons of the class ammunition, they will share the same ammount of bullets which does not make sense.

$$anonymous$$oreover, you use the static keyword, but you retrieve a class instance using GetComponent. What is the point? If you retrieve the class instance, you don't need the static keyword anymore.

avatar image shadowpuppet Hellium · Nov 17, 2017 at 08:17 PM 0
Share

oh, ok. I'm pretty new at this and I had asked a very similar question some time ago and that was the answer i got. or pretty similar anyway. This is not how I handle my shooting scripts it was just an example off the top of my head, but I don't get how if I had two ammo clips with the script "ammunition" on them that they share bullets. I mean if I shoot my gun and that gun is referencing a specific clip with the script ammunition on it how it would effect the script on another game object? I usean ammo$$anonymous$$anager script that keeps track of grenades, flares, pistol bullets, machine gun bullets and C4. Then a script on each that references the ammo$$anonymous$$anager and takes away the particular ammo I used when I used it. If i am holding a pistol i deplete the pistol bullets on fire button pressed, machne gun depletes machine gun bullets etc etc

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

130 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 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 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 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 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 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 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 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 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 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

Script executing through walls 1 Answer

loop vs if spell casting script 1 Answer

Slowly increase joint limit over time 1 Answer

How to rotate a cube on its edge and have the cube able to climb up other surfaces 0 Answers

How to tell if a Canvas UI slider is being clicked not over Value 2 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