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 ppddgg · Jul 02, 2012 at 08:48 PM · c#getcomponentvariables

C# Access to a variable by another script attached in a gameObject in the scene.

I want to access to a variable of the script "ExampleScript", which is put in a gameObject in the scene, called "player", and using it in the script "AccessVariabileScript".

This is the Player Script where there is the variable MyMoreSpeed:

 using UnityEngine;
 using System.Collections;
 
 public class ExampleScript : MonoBehaviour {
  
  //SpeedBall 
     public int myMoreSpeed = 0;
 
     void OnTriggerEnter(Collider other){
     if(other.gameObject.name == "MoreSpeed"){
  myMoreSpeed++;
  }
  
  }

Now, in another Script I want to access to the variable myMoreSpeed and, if this variable is >= 1 do somthing, in that case by touching the screen of an touchscreen and, if the myMoreSpeed is >= 1, the speed change.

I tried to do a c# code by myself. But it was useless.

This is the script:

 using UnityEngine;
 using System.Collections;
 
 public class AccessVariableScript : MonoBehaviour {
 
 public  float speed = 1; //<-- Normal speed
 public float speedUP = 2; //<-- If all the conditions are true, speed = speedUP
 public float speedA = 1;
 
 void Awake(){
  GameObject myPlayer = GameObject.Find("Player"); //<-- I tried to find the gameObject in the scene
  SpeedA = myPlayer.GetComponent<ExampleScript>().myMoreSpeed; //<--  ????
  
  }
 
 void Update () {
  //Simple movement 
  transform.Translate(0,-speed,0);
     //MoreSpeed. In this part of code I don't undertand why if I don't put "SpeedA >=1" works, but if I put it , doesn't work.
  if( SpeedA >= 1 /* I don't know if this is correct */ & Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began){
  speed = SpeedUP;
 }
 }

Unity3d doesn't report any error but the script doesn't work as I want.

Another question I want to ask you is how can I decrease The variable myMoreSpeed by 1 point, when the player touch the screen of an Touchscreen?

Thanks for the attention and if you found any typos I apologize, I'm Italian and I don't speak english very well.

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 harko12 · Jul 02, 2012 at 10:45 PM

Well, in your find, you have a capital P on player, is that how the gameobject is? If the Find fails, then there's no way the GetComponent could work either. Make sure that is the right string that you're searching for. You could put a tag on your player then try FindGameObjectWithTag as well.

as for your second question, I think myMoreSpeed-- will reduce the speed by one, the same way myMoreSpeed++ increases it.

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
0

Answer by ppddgg · Jul 03, 2012 at 05:49 PM

I tried in another way. I create a "`public GameObject myGameObject`";

In unity I put the GameObject with the script in the "myGameObject".

After do that I call the script with "`myGameObject.GetComponent();`".

Finally works. Thanks!

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Getting a Variable on Script B to equal a variable from script A 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Using a variable value with GetComponent 1 Answer

fix script to get C# var from another object instead of object this script is attached to 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