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 KingGeorgeXVIII · Jul 24, 2014 at 11:01 AM · c#variablesprefab-instance

How do I access variables from a game object from an instantiated prefab

I have a power up on my game, that when it is activated it instantiates a few of a prefab that I made. When the prefabs collide with an object I want points to be added to the player's score. I have tried all kinds of stuff to get the script on the instantiated prefabs to interact with the script that I have on an empty gameobject called GameControl that holds all the score info, but nothing is working for me. I think this is the closest I have gotten to getting the end result that I want. This script is on my prefab and is attempting to edit the TotalCount variable on my empty game object.

 void Start()
     {
         GameObject.Find("GameControl").GetComponent<PointCounterScript>();
         
         GameObject PointCounter = GameObject.Find("GameControl");
         Component OverallPoints = PointCounter.GetComponent<PointCounterScript>();
     }
 
 
     void OnTriggerEnter(Collider Other)
     {
         if(Other.gameObject.tag == "Person")
         {
             OverallPoints.TotalCount = OverallPoints.TotalCount + 1;
         }
     }
 }
 

I get two error messages when I try to run this. They are both "error CS0103: The name `OverallPoints' does not exist in the current context". One error for each "OverallPoints" I used on line 23. Any ideas on what I am doing wrong? Or suggestions for a better way to accomplish this? I am new to C#, so I apologize in advance if it is something obvious!

Comment
Add comment · Show 1
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 sethuraj · Jul 24, 2014 at 11:26 AM 0
Share

I didnt fully understand your problem.As far as I understoor,the error you are getting because of a typemismatch i guess.try changing this line

 Component OverallPoints = PointCounter.GetComponent();

to this

 PointCounterScript OverallPoints = PointCounter.GetComponent<PointCounterScript>();

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DajBuzi · Jul 24, 2014 at 11:18 AM

You have declared variable in the Start method and tried to use it in the OnTriggerEnter method. Rewrite the code and put Component OverallPonts on top of the class and then you can access it in other methods ;)

Comment
Add comment · Show 1 · 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 KingGeorgeXVIII · Jul 25, 2014 at 01:16 AM 0
Share

Thanks! I kept the code the same and moved both of the lines with the GameObject and Component to the top of the class. And I think it worked. But now I get this error message " error CS0236: A field initializer cannot reference the nonstatic field, method, or property `StampedeUnicornScript.PointCounter'"

It is on the line with the

  Component OverallPoints = PointCounter.GetComponent<PointCounterScript>();

Do you know what that is about?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Is it possible to access a variable in a different script without reference? 2 Answers

Increase difficulty Space Shooter 2 Answers

Changes to prefab instance generated from script aren't remembered 1 Answer


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