• 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

Nevermind 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Access variables from another script 1 Answer

Refering to gameobject script is attached to 1 Answer

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

Can't get script to access other script/class (c#) 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges