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 CoCoMolly · Oct 17, 2012 at 10:20 PM · variablegetcomponentparentchild

How can i make GetComponent(SCRIPT) apply to ONLY ONE object that has the script?

Ive looked and looked. Tried and tried. I dont understand why this wont work.

Say you have 100 objects with the same script.

Each of those Objects has children.

If i mouse click on a child, i want that child to:

1:Identify its parent

2:Change a variable of the parent's script

3.Then destroy itself

The problem is when i click on the child, it changes all 100 object's variables that have the same script. I am using:

target = transform.parent.GetComponent(ParentScript);

target.BooleanVar = true;

target.IntegerVar = 1;

Thanks abunches! :)

Comment
Add comment · Show 2
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 Bunny83 · Oct 17, 2012 at 10:23 PM 0
Share

This is not enough information. How do you detect your "clicks"? Please add all relevant parts of your script to the question. At the moment the question can't be answered.

Feel free to edit your question

avatar image Fattie · Oct 18, 2012 at 07:18 AM 0
Share

go to unitygems.com for a full explanation and tutorial of GetComponent

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by hvilela · Oct 17, 2012 at 11:02 PM

I can see two possible causes:

  1. Your variables at "ParentScript" are static, meaning that they belong to the class, not to the instance.

  2. You have an error in your click detection function and your script is changing it all.

Without your click detection and your ParentScript codes, it's impossible to tell.

Comment
Add comment · Show 4 · 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 CoCoMolly · Oct 17, 2012 at 11:21 PM 0
Share

YES!! $$anonymous$$y variables in my ParentScript are static. So should I make them public??? Thanks so much for helping me. (Even with the little information I provided ha )

avatar image hvilela · Oct 17, 2012 at 11:27 PM 0
Share

Yes, remove the static keyword. Static means it belongs to the class, or in other works, that you have just one "BooleanVar" and one "IntegerVar" shared between all the script instances.

avatar image CoCoMolly · Oct 18, 2012 at 03:36 AM 0
Share

Yes! Thanks so much!

avatar image hvilela · Oct 18, 2012 at 03:38 AM 0
Share

Sure, no problem.

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

12 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

Related Questions

Talking To Other Scripts 1 Answer

Make a simple tree 1 Answer

Accessing parent gameobject script not working 1 Answer

Get variable from another object 1 Answer

Putting a variable of a child in the inspector for the parent 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