Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Rembo4Fight · Feb 22, 2018 at 08:38 PM · specificthis

How to get the specific value from GameObject?

I'm trying to get how to make this. I have my collectible item which have special value But they all have one script and their value is typing in unity hierarchy How can I can the specific value in script?please explain Maybe "this" method or something like it...

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

Answer by SwedishDerp · Feb 23, 2018 at 08:31 AM

@Rembo4Fight

Not really sure what you are looking after but to recieve values and other commands from a gameobject with a script attached you can easily go with "GetComponent". Kinda like this:

 public GameObject theObj;
 public float Value;
 
 public void Update(){
 
 if(Input.GetKeyDown(KeyCode.E)
 {
 Value = theObj.GetComponen<The Script>().Value;
 }
 
 }
Comment
Add comment · Show 11 · 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 Rembo4Fight · Feb 23, 2018 at 08:38 AM 0
Share

Thanks for your reply I have two values for different collectible objects And I type this values on the script component in the hierarchy The question is about how to get this specific values alt text alt text

2.jpg (27.9 kB)
1.jpg (28.9 kB)
avatar image Rembo4Fight · Feb 23, 2018 at 08:42 AM -1
Share

You see, I have this two scripts if it might help! You can see that on the line 19 I'm using what your advice was alt text alt text

4.jpg (42.1 kB)
3.jpg (86.8 kB)
avatar image SwedishDerp · Feb 26, 2018 at 07:32 AM 1
Share

In the collectibles script you can not get the specific values by acessing the script itself, you need to have a owner.

Ins$$anonymous$$d of:

 Score$$anonymous$$anager.score = this.score;

Use this:

 col.GetComponent<Score$$anonymous$$anager>().score += this.score;


By doing this we are accessing the collider object's scripts :)

avatar image Rembo4Fight SwedishDerp · Feb 26, 2018 at 08:47 AM 0
Share

I have this static value, how can I change this to access?

 public static int score;
avatar image Rembo4Fight SwedishDerp · Feb 26, 2018 at 09:59 AM 0
Share

Okay, so I changed the script like how you type it and change "public static int score" to just "public int score" But ins$$anonymous$$d of += this.score I think there must to be a += this.scoreValue right?

So here whats it look like now

             //Score$$anonymous$$anager.score += scoreValue;
             col.GetComponent<Score$$anonymous$$anager> ().score += this.scoreValue;
             ScoreWorldText.printingScore = this.scoreValue;
             Score$$anonymous$$anager.collected = true;

In the Game mode the score world text showing only 100 points And the error in the console appeared alt text

screen-shot-2018-02-26-at-125515-copy.jpg (42.0 kB)
avatar image SwedishDerp Rembo4Fight · Feb 26, 2018 at 11:18 AM 0
Share

No, no. But almost!

 //Score$$anonymous$$anager.score += scoreValue;
              col.GetComponent<Score$$anonymous$$anager> ().score += scoreValue;
              col.GetComponent<Score$$anonymous$$anager>().printingScore = scoreValue;
              col.GetComponent<Score$$anonymous$$anager>().collected = true;

You see, the GetComponent function needs an owner with this script and in this case it's the collider (col). You can google further on the "GetComponent" function :) Sorry if I'm unclear

Show more comments
avatar image
1

Answer by Vencarii · Feb 26, 2018 at 09:23 AM

Wouldn't it be better to have Collectibles script with a List or Array that holds all the Collectible objects instead of two instances of that Script on the GameObject?

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 Rembo4Fight · Feb 26, 2018 at 10:02 AM 0
Share

Is that will be easier? I'm not so good in scripting for now, don't know many things :) I need just to make the collectibles to show specific value, and not updating this value when other collectible is collected because it have his own score text to be shown.

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

76 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Difference between gameObject and GameObject and a scene item named "GameObject" and this in context of Unity grammar 2 Answers

Having issue with an object reference is required to access a non-static member. 2 Answers

Click To Delete Specific Object 1 Answer

moving an object with a specific speed 1 Answer

Show object for a specific platform only. 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