Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 game_crusher · Jun 26, 2015 at 11:09 AM · unity 5unity 2daccessing from any script

Can't acces a variable from another script

Well actually it seems i can(Unity console doesn't trow any errors) but the code that does actually something with that variable doesn't work.I have a script called HUDScript and in it the float variable scorePlayer.I declared them both public in that script.Now i have another script(CameraRunnerScript) which constantly moves the camera to the right(platformer/infinite runner).I want to increase the moving speed of the camera once the score is above a number(for now 300).I called the playerScore variable from the HUDScript in the CameraRunnerScript but i believe i'm not doing it wright. So here's the code:

 public class CameraRunnerScript : MonoBehaviour {
     float playerScore;
     public Transform player;
 
 
     // Use this for initialization
     void Start () {
          
           playerScore = GameObject.Find ("Main Camera").GetComponent<HUDScript>().playerScore;
 
     }
 
     // Update is called once per frame
     void Update () {
 
         transform.position = new Vector3 (transform.position.x + 0.15f, 0, -10);
 
         if(Time.timeScale == 0) 
         {
             transform.position = new Vector3 (transform.position.x-0.15f , 0, -10);
         }
         if ((int)(playerScore) >= 300)
         {
             transform.position = new Vector3 (transform.position.x + 0.5f, 0, -10);
         }
     }
 }

That last if condition is never tested since it doesn't do anything. It doesn't recognize the playerScore variable from the HUDScript? If so why doesn't the console give me any errors? Thank you!

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

Answer by Hellium · Jun 26, 2015 at 11:18 AM

It's normal, in the start function, your are making a copy of the value of HUDScript.playerScore, and your are not getting a reference on it.

I would advise you to check the if( playerScore >= 300f ) ... condition inside the HUDScript script. In the HUDScript, add a public variable of type CameraRunnerScript and drag your camera in the field which appeared in the inspector.

When the condition is verified, call the function IncreaseSpeed() of your CameraRunnerScript defined as follow :

 public void IncreaseSpeed()
 {
      if( currentSpeedIndex < Speeds.Length - 1 )
             currentSpeedIndex++ ;
 }


The variables involved are defined as follow :

 // Add the different speeds directly from the inspector
 public Vector3[] Speeds ;

 // Index of the speed
 private int currentSpeedIndex = 0

 // Current speed of the camera = Speeds[currentSpeedIndex]
 

In your Update function, you will just have to write this :

 if(Time.timeScale == 0) 
 {
     transform.position = new Vector3 (transform.position.x-0.15f , 0, -10);
 }
 else
 {
     transform.Translate( Speeds[currentSpeedIndex] ) ;
 }



Full C# script :

 public class CameraRunnerScript : MonoBehaviour
 {
     public Transform player;
     
     // Add the different speeds directly from the inspector
      public Vector3[] Speeds ;
      
      // Index of the speed
      private int currentSpeedIndex = 0 ;
 
     // Update is called once per frame
     void Update () {
 
         transform.position = new Vector3 (transform.position.x + 0.15f, 0, -10);
 
         if(Time.timeScale == 0) 
         {
             transform.position = new Vector3 (transform.position.x-0.15f , 0, -10);
         }
         else
         {
             transform.Translate( Speeds[currentSpeedIndex] ) ;
         }
     }
     
     // Called by HUDScript every time the player score is above a new step
     public void IncreaseSpeed()
     {
         if( currentSpeedIndex < Speeds.Length - 1 )
             currentSpeedIndex++ ;
     }
 }

 public class HUDScript  : MonoBehaviour
 {
     // Drag the camera holding the CameraRunnerScript from the inspector
     public CameraRunnerScript CameraRunnerScript ;
     
     public float playerScore ;
 
     // Update is called once per frame
     void Update () {
 
         ...
         
         if( playerScore > 300 )
         {
             CameraRunnerScript.IncreaseSpeed() ;
         }
         
         ...
     }
 }

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 game_crusher · Jun 26, 2015 at 12:09 PM 0
Share

Thanks Hellium, i copied the code,it seems there is something wrong in the HUDScript code, error:

Assets/Scripts/HUDScript.cs(7,20): error CS1519: Unexpected symbol `float' in class, struct, or interface member declaration

Line 7 is: public float playerScore; right under: public CameraRunnerScript Some syntax mistake?

avatar image Hellium · Jun 26, 2015 at 12:11 PM 0
Share

$$anonymous$$y fault, sorry :

public CameraRunnerScript CameraRunnerScript ;

avatar image game_crusher · Jun 26, 2015 at 12:30 PM 0
Share

It seems i don't get it how it works.I have an array off speeds on the inspector of $$anonymous$$ain Camera.I choose 2 speeds,in the first i put a 0.15 value of X,in the second a 0.5 value of X.It starts the game with directly the second value of x(not increasing anything when score is >300).Sorry for the trouble.What should i do now?

avatar image Hellium · Jun 26, 2015 at 12:43 PM 0
Share

It seems your currentSpeedIndex is equal to 1 at the beginning.

$$anonymous$$ake sure to declare as follow :

private int currentSpeedIndex = 0 ;

Never touch this attribute outside the CameraRunnerScript and call CameraRunnerScript.IncreaseSpeed() only from your HUDScript.

avatar image game_crusher · Jun 26, 2015 at 12:56 PM 0
Share

If i let the speeds size to 0 in the Inspector i get a bunch off: IndexOutOfRangeException: Array index is out of range. CameraRunnerScript.Update () (at Assets/Scripts/CameraRunnerScript.cs:53) Line 53 is: transform.Translate( Speeds[currentSpeedIndex] ) ; in if/else condition

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Can anyone help with loading issues when button is clicked? 0 Answers

How do I get the square root of a double number value? 2 Answers

Can I assign plus license to unity 5.3.4p3 1 Answer

Directional movement of the character along the arrow,Направленное движение персонажа по стрелке 0 Answers

Convert r2d.position.x to Pixels UnityScript 0 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