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
1
Question by Stormizin · Aug 11, 2013 at 09:30 PM · c#gameobjectvariableaccess

How to access gameObject variable script

I have 3 GameObjects in my screen. if i hit a particular gameObject i need to access his variable and change the variable(count), which started with zero, to 1, then 2, then 3 ... etc.

How can i access this variable to make changes? Some hint in C#?

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

Answer by cmpgk1024 · Aug 11, 2013 at 09:38 PM

Use GameObject.GetComponent to return the script, and then set its variable count to whatever you want. For example:

Counter counter = GameObject.Find("Your GameObject").GetComponent();
counter.count++;

You will have to add using System.Collections.Generic for the above to work.

Comment
Add comment · Show 2 · 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 Stormizin · Aug 11, 2013 at 09:54 PM 0
Share

And if i have the same script attached to the 3 gameObjects?

avatar image Stormizin · Aug 11, 2013 at 09:56 PM 0
Share

Never$$anonymous$$d figured this out ... just counter = hit.collider.gameObject.GetComponent();

avatar image
0

Answer by Joyrider · Aug 11, 2013 at 09:38 PM

You can do it two ways:

Either the script does it on it's own on every object using OnMouseEnter, OnMouseExit and OnMouseButtonDown

 bool mouseIsOverObject = false;
 bool thisObjectsCounter = 0;
 
 void OnMouseEnter()
 {
    mouseIsOverObject = true;
 }
 
 void OnMouseExit()
 {
    mouseIsOverObject = false;
 }
 
 void Update()
 {
    if(Input.GetMouseButtonDown(0)&&mouseIsOverObject )
        thisObjectsCounter++;
 }

or

You have a script on your camera that uses raycast, raycasthit to get the hit object; your 3 objects than need colliders though. That way you get the hit object, then you can access it's script with GetComponent and then add one to your particular variable.

 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
 RaycastHit hit;
 if (Physics.Raycast(ray, out hit, 100))
 {
    if(hit!=null)
       hit.collider.gameObject.GetComponent<MyScriptClass>().thisObjectsCounter++;
 }

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

17 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

Related Questions

Change and Access another scripts Variables 1 Answer

Refering to gameobject script is attached to 1 Answer

My marble (Player/Gameobject) goes through the "play board" (gameobject). Why? 1 Answer

Accessing a variable effected by a GUI slider from another scipt 1 Answer

Is it possible to change a variable, into a script not assigned to any game object? 3 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