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
-1
Question by mcmalonzo · Sep 19, 2017 at 06:44 PM · variablechange

trouble changing variables of another script

hi! I have a problem: can't change the variable "count" of the second script from the first one. I guess is an easy problem... but I've started programming just last week. ^_^' (the scripts should create an exit from the level when you've killed 40 enemies)

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Zombie4Beha : MonoBehaviour { public GameObject contatore, player; private float speed; private Rigidbody2D rb; private int pv;

 void Start()
 {
     gameObject.tag = "zombie4";
     rb = GetComponent<Rigidbody2D>();
     rb.drag = 10;
     pv = 4;
     speed = 5;
 }

 void Update()
 {
     GameObject player = GameObject.FindGameObjectWithTag("character");
     rb.rotation = 0;
     Vector2 proprio = new Vector2(rb.position.x, rb.position.y);
     Vector2 dir = new Vector2(player.GetComponent<Rigidbody2D>().position.x, player.GetComponent<Rigidbody2D>().position.y);
     rb.AddForce(-(proprio - dir).normalized * speed);
     if (pv <= 0)
         Destroy(gameObject);
 }

 private void OnTriggerEnter2D(Collider2D coll)
 {
     if (coll.gameObject.tag == "proiettile")
         pv = pv - 1;
 }

 private void OnDestroy()
 {
     contatore.GetComponent<DoorpawnBeha>().count = contatore.GetComponent<DoorpawnBeha>().count + 1;
 }

}

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class DoorpawnBeha : MonoBehaviour { public int count; public GameObject porta; public Transform oggSpawn;

 private void Start()
 {
     count = 0;
 }

 private void Update()
 {
     Debug.Log(count);
     if (count >= 40)
         Instantiate(porta, oggSpawn.position, oggSpawn.rotation);
 }

}

thanks from now for the help!

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
Best Answer

Answer by PersianKiller · Sep 19, 2017 at 07:00 PM

hey

script 1

 public DoorpawnBeha name;

 void Start(){
 name=FindObjectOfType<DoorpawnBeha>();
 }


so now you can access to DoorpawnBeha script with calling name.

for example in your first script you can now use something like this

 void Update(){
 if(name.score>40){
 //do something
 }
Comment
Add comment · Show 5 · 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 mcmalonzo · Sep 19, 2017 at 07:09 PM 0
Share

thanks so much!!!

avatar image PersianKiller mcmalonzo · Sep 19, 2017 at 07:15 PM 0
Share

yw dude :)

avatar image mcmalonzo PersianKiller · Sep 22, 2017 at 08:30 PM 0
Share

lol, after only 3 days I had already forgotten that XD

avatar image maxance710 · Jan 17, 2018 at 11:45 AM 0
Share

i have the same problem, did exactly what you said to do, but it still doesn't work for me. any idea why??

avatar image PersianKiller maxance710 · Jan 17, 2018 at 12:33 PM 0
Share

@maxance710 look at this example.

I want when I get 100 scroes in script 1,then another script show me a message like this (Yay)

* script1

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;

 public class script1 : $$anonymous$$onoBehaviour {
 public int score;
 // Use this for initialization


 void OnTriggerEnter2D(Collider2D other){
     if(other.CompareTag("coin")){
         score += 10;
         Destroy (other.gameObject);

     }
 }
 
     }

* script 2

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;

 public class script2 : $$anonymous$$onoBehaviour {
 public script1 s1;
 // Use this for initialization
 void Start () {
     s1 = FindObjectOfType<script1> ();
     
 }
 
 // Update is called once per frame
 void Update () {
     if (s1.score > 100) {

         Debug.Log ("YAY");
     }
     
  }
 }




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

120 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

Related Questions

Variable value does not change 0 Answers

Switch Position of two moving objects 1 Answer

Inspector value changed automatically to an old value on runtime 0 Answers

Cant change variable speed in script 1 Answer

Make a float variable change when another variable changes? 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