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 edouinj · Dec 18, 2018 at 09:04 PM · script.variablevariablesobjects

C# taking variable from other script

Hello, I started using unity few weeks ago and now I have a problem with taking variable from another script. Once I pickup object "Cherry" in-game my variable "knivedelay" should increase, however i have tried doing it but it did not update, can someone help me. I have tried doing it with game objects but it did not work. Thanks in advance. Spawning.cs :

 using System.Collections;
 using System.Collections.Generic;
 using System.Threading;
 using UnityEngine;
 
 public class Spawning : MonoBehaviour {
 
     public float coindelay = 1f;
     public float knivedelay = 4f;
     public GameObject knive;
     public GameObject coin;
     // Use this for initialization
     void Start () {
         InvokeRepeating("CoinSpawn", coindelay, coindelay);
         InvokeRepeating("KniveSpawn", knivedelay, knivedelay);
         print(coindelay);
     }
 
     //Prefabs spawning
     void CoinSpawn () {
         Instantiate(coin, new Vector3(Random.Range(-10,10), 12, 0), Quaternion.identity);
     }
 
     void KniveSpawn()
     {
         Instantiate(knive, new Vector3(Random.Range(-10, 10), 12, 0), Quaternion.identity);
 
     }
 }

CherryPickUp.cs :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class CherryPickUp : MonoBehaviour
 {
 
     public static bool isCherry = false;
     public static float cherryScore = 0.0f;
     // Update is called once per frame
 
     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.tag == "Player")
         {
             isCherry = true;
             Destroy(gameObject);
             cherryScore += 10;
             GameObject.Find("TheCherry").GetComponent<Spawning>().knivedelay -= 0.5f;

             /*for (int i = 0; i < n; i++)
             {
                 Spawning.coindelay = Spawning.coindelay + 10f;
                 Debug.Log(Spawning.coindelay);
             }
         }
         else {
             isCherry = false;
         }
         Debug.Log(isCherry);
        /* while (true)
         {
             if (isCherry == true)
             {
                 Spawning.coindelay = Spawning.coindelay + 5f;
             }
         }
 
         /*while (isCherry == true)
         {
             Spawning.coindelay += 5f;
         }*/
 
         }
     }
 }
   
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

Answer by JonPQ · Dec 18, 2018 at 09:23 PM

your problem is here.... GameObject.Find("TheCherry").GetComponent().knivedelay -= 0.5f;

For this to work, there must be object named "TheCherry" with component Spawning on that object (not on a parent or child object) This is also hard to debug or step through one line at a time to see what is wrong. It is also very,very slow to execute.... really avoid using GameObject.Find and avoid using GetComponent
Try to only use them during the Start() and Awake() functions, so they are used on setup only, and not all of the time. Even better if you expose public variables and setup the references to the other components on a prefab (if possible)

split that line into 3 lines with intermediate, temporary variables, and it will be easy to debug to see which step is failing. GameObject obj = GameObject.Find("TheCherry"); if(obj !=null) Spawning spwn = obj.GetComponent() else Debug.Log( "cant find TheCherry"); etc...

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
avatar image
1

Answer by Hellium · Dec 18, 2018 at 09:28 PM

Supposing you don't get any NullReferenceException, once InvokeRepeating is called, even if you change the parameter of the delay, it won't be taken into account. You need to use a coroutine instead

 public class Spawning : MonoBehaviour {
  
      public float coindelay = 1f;
      public float knivedelay = 4f;
      public GameObject knive;
      public GameObject coin;
      
      // Use this for initialization
      void Start ()
      {
          StartCoroutine( SpawnCoins() );
          StartCoroutine( SpwanKnives() );
      }
  
      //Prefabs spawning
      IEnumerator SpawnCoins ()
      {
          while( coindelay > Mathf.Epsilon )
          {
             yield return new WaitForSeconds( coindelay );
             Instantiate(coin, new Vector3(Random.Range(-10,10), 12, 0), Quaternion.identity);
          }
      }
  
      IEnumerator SpwanKnives()
      {
          while( knivedelay > Mathf.Epsilon )
          {
             yield return new WaitForSeconds( knivedelay );
             Instantiate(knive, new Vector3(Random.Range(-10, 10), 12, 0), Quaternion.identity);
          }
  
      }
  }
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

110 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

Related Questions

Set a script as a variable in C# 2 Answers

How can I improve my trading script? 1 Answer

How to make variables change for only one item having the script 0 Answers

Global Variables Refuse to Cooperate 1 Answer

How can I set the default position of a 3D object? 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