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 $$anonymous$$ · Jan 31, 2014 at 12:00 AM · c#variableinstance

C# How to access a variable in a script from an instance with the same script?

Hello.

I have set up some code which instantiates and gives tags to a couple of objects. This works fine.

When one of the objects are clicked on, its tag is stored idStart = gameObject.tag; when another object is clicked I store its tag in the same manner.

So to avoid confusion. Object 1 is pressed, object 2 is pressed. Object 2's script then changes the variable in object 1's script. How would I go about doing this.

This is the code in the script where everything is to be handled. At the bottom, the line dotScript idStart.dotValue--; is supposed to change the value of object 1's dot value

 if (!isClicked)
         {
             isClicked = true;
             //dotValue-=10;
             idStart = gameObject.tag;
             transform.localScale = new Vector3 (dotValue / 100, dotValue / 100, dotValue / 100) * 5;
             xStart = transform.position.x;
             yStart = transform.position.y;
             zStart = transform.position.z;
         }
         else
         {
             //dotValue+=10;
             idEnd = gameObject.tag;
             c = 10;
             transform.localScale = new Vector3 (dotValue / 100, dotValue / 100, dotValue / 100) * 5;
             isClicked = false;
             xEnd = transform.position.x;
             yEnd = transform.position.y;
             zEnd = transform.position.z;
             for (int i = 0; i<=c;i++)
             {
                 Instantiate(capsualPrefab,new Vector3(xStart+Random.Range(0.1f,1.9f),yStart+Random.Range(0.1f,1.9f),zStart+Random.Range(0.1f,1.9f)),Quaternion.identity);
                 dotScript idStart.dotValue--;
             }
         }

Any help is appreciated.

Thanks for your time and I will supply any further information if it is required.

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
0

Answer by getyour411 · Jan 31, 2014 at 12:08 AM

Take a look at some tuts on doing this,

http://unitygems.com/script-interaction1/

 GameObject newObj = GameObject.FindObjectWithTag("someTag").GetComponent<someClass>();

or similar structure, depending on your needs. That site has great stuff.

Post back if that site doesn't get you moving

Comment
Add comment · Show 3 · 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 $$anonymous$$ · Jan 31, 2014 at 11:53 AM 0
Share

Thanks for the help, however, I cannot see why the line of code you are giving me is giving me an error Assets/dotScript.cs(53,88): error CS1061: Type UnityEngine.GameObject[]' does not contain a definition for GetComponent' and no extension method GetComponent' of type UnityEngine.GameObject[]' could be found (are you missing a using directive or an assembly reference?)

That site was very helpful for understanding but did not help with progressing my problem.

avatar image getyour411 · Jan 31, 2014 at 05:31 PM 0
Share

Show me the exact lines you are trying? It looks like there's something in an Array which I didn't have referenced in my example.

avatar image $$anonymous$$ · Jan 31, 2014 at 08:11 PM 0
Share

This is the line I am trying.

 GameObject newObj = GameObject.FindGameObjectsWithTag(idStart).GetComponent<dotScript>();

It seems to have a problem with GetComponent right at the end. I do have an array in use either.

EDIT:

This line was wrong because I put an s in by mistake. However, I am apparently trying to convert type dotScript to a GameObject? Is this correct according to the code I have given.

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

Multiple Cars not working 1 Answer

Javascript not being updated, variables being overridden, but C# is fine 1 Answer

Can't access the script of an instantiated prefab :( 2 Answers

Editing a variable from another script on collision 3 Answers

Using a script as a member variable in another script. 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