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 /
  • Help Room /
avatar image
0
Question by SQUARE-BADGER · Aug 21, 2016 at 09:33 PM · variablevariablesscriptingbasics

how to controll a variable of a script from another script

i am kinda dum at this and i need some help, i have a script and i wanted to change a int variable from another script

i have a script when a specific object is destroyed and i want to ad a "change the variable of the x script to -1when destroyed or something "

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
0

Answer by ArturoSR · Aug 21, 2016 at 10:20 PM

Hi, ok, this is what you need to do, the easy way it is to get access to the script component from the other object, something like this:

 //Object 1 - Component "Shield"
 public int damage = 0;
 
 void Update {
     if (damage >= 20) {
         Destroy(this.gameObject);
     }
 }
 
 //Object 2 - Component "Weapon"
 Shield componentObject1 = null;

 void Update() {
     if (componentObject1 != null) {
         componentObject1.damage += Random.Range (1, 5);
     }
 }

 void OnTriggerEnter(Collider collider) {
     //Here you can get the object that enter on the trigger and get its component.
     componentObject1 = collider.gameObject.GetComponent<Shield>();
 }

 //Remember, this is just an example, not already your code.

Only you have to do it is to drag and drop the "other" object with the Shield component attached to it, you will notice that the variable type from the Object 2 has the same "name" as the script component from the Object 1, this is known as Class and you can reference any public class, you can gain a direct access to any field, property, method or function with this.

Using this method you can modify any variable from any script component, remember to make any target variable or function public to gain access to it, cheers.

Comment
Add comment · Show 7 · 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 SQUARE-BADGER · Aug 23, 2016 at 08:34 AM 0
Share

ok, i made this code but my int zombie amount does not change

 using UnityEngine;
 using System.Collections;
 
 public class EnemyBehaviour : $$anonymous$$onoBehaviour
 {
     SpawnScript componentZombieSpawn = null;
     public int life = 100;
 
     // Use this for initialization
     void Start()
     {
 
     }
 
     // Update is called once per frame
     void Update()
     {
         if (componentZombieSpawn != null)
         {
             componentZombieSpawn.zombie_amount -= 1;
         }
         if (life <= 0)
         {
             Destroy(this.gameObject);
             
         }
     }
 
     void OnTriggerEnter(Collider Bullet)
     {
         life -= 10;
         print("am fost lovit " + Bullet.name);
 
     }
 }

and the one with the variable

 using UnityEngine;
 using System.Collections;
 
 public class SpawnScript : $$anonymous$$onoBehaviour {
     public int zombie_amount = 4;
     public GameObject zombie;
 
     // Use this for initialization
     void Start () {
         Instantiate(zombie, transform.position, transform.rotation);
     }
     
     // Update is called once per frame
     void Update () {
         if (zombie_amount <= 0)
         {
             Instantiate(zombie, transform.position, transform.rotation);
             zombie_amount = 4;
         }
     }
 }
 




avatar image ArturoSR SQUARE-BADGER · Aug 23, 2016 at 11:49 AM 0
Share

What do you get using this?, perhaps you forgot to add the Instantiated Zombie to the SpawnerScript variable.

avatar image SQUARE-BADGER ArturoSR · Aug 23, 2016 at 12:13 PM 0
Share

no error so far, but the zombie_amount remains the same

Show more comments
Show more comments
avatar image
0

Answer by SQUARE-BADGER · Aug 25, 2016 at 11:06 PM

ok so i put a debug on the void update and it still does not react, the zombie spawn object does not change its zombie_amount int, and the debug does not show anything i tried to solve this but i am stuck with this 3 days in a row

 void Update()
      {
          if (componentZombieSpawn != null)
          {
              componentZombieSpawn.zombie_amount -= 1;
          }
          if (life <= 0)
          {
              Destroy(this.gameObject);
              
          }
      }
   
Comment
Add comment · Show 3 · 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 SQUARE-BADGER · Aug 25, 2016 at 11:13 PM 0
Share

i dont realy get it

avatar image ArturoSR · Aug 26, 2016 at 03:18 AM 0
Share

Have you Skype or Facebook?, I like to make a more direct contact with you, this way I can help you to resolve you problem more quickly, what do you think?.

avatar image SQUARE-BADGER ArturoSR · Aug 26, 2016 at 09:45 AM 0
Share

good idea, Andrei Nica on facebook and nica.andy1 on skype

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

69 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

Related Questions

Variable from another script isn´t updating 3 Answers

Assigning variables depending on other variables 1 Answer

Problem with the Inspector and Script 1 Answer

Help with variables 1 Answer

Use enum hidden value as something other than int? (C#) 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