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 Avarthar · Jan 10, 2017 at 06:48 AM · c#getcomponentcomponentguitextskills

Problem with getting component, help please? ^^'

Hi I've got some trouble lately trying to use getcomponent.

Could I have some information on what makes my C# script not working please? ^^'

 static Component skillLvlVal;
 static Component skillCurVal;

 void GetValues() {
     skillLvlVal = transform.parent.gameObject.GetComponent("LvlVal");
     skillCurVal = transform.parent.gameObject.GetComponent("CurVal");
 }

 static int lvlVal = int.Parse(skillLvlVal.GetComponent<GUIText>().text);

 public static int currentVal = int.Parse(skillCurVal.GetComponent<GUIText>().text);

 static int minHp;
 static int maxHp;
 static int hp;

 static void Start () {
     GetValues();
     minHp = 0;
     maxHp = lvlVal;
     hp = maxHp;
 }

This is only a part of the script, but the rest isn't necessary as it's not yet using those values, but will as soon the error is fixed ^^.

Basically I'm trying to get a component (LvlVal and CurVal) from the script parent. But when calling it, it gives me an "Object not set to an instance of an Object." for each of the two Values.

This script is to make a basic Skill tree. (Here's the screen shot to show what it looks like, I got a button on the second button (second in the top row)) alt text

Here's how the button looks like, the CurVal is the top number and the lvlVal is the bottom one. alt text

Could I get some help with that please ? ^^

skill-panel.png (41.7 kB)
skill01-test01.png (3.5 kB)
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
1
Best Answer

Answer by tanoshimi · Jan 10, 2017 at 07:05 AM

This line gives you an "object not set to an instance of an object"?

 skillLvlVal = transform.parent.gameObject.GetComponent("LvlVal");

That would suggest that the object to which this script is attached does not have a parent.

As a separate issue though, you seem to be making all your variables static- why is that? It's undoubtably going to cause you troubles later on.

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 Avarthar · Jan 10, 2017 at 04:51 PM 0
Share

would there be a better way to do it? ^^ (I'm kinda new to c# coding ^^') Here's the error it gives me when I remove the statics:

alt text

avatar image Avarthar Avarthar · Jan 10, 2017 at 04:36 PM 0
Share

Oh, I think I found what you meant by no parent. It tries to get the parent of object that has the script on.

Now I get it. ^^ Still trying to fix it, but yea... now I understand a bit more ^^.

avatar image tanoshimi Avarthar · Jan 10, 2017 at 05:07 PM 0
Share

Well, the better way to do it would be to not make those variables static unless you really think they need to exist at the class level rather than at the instance level.

avatar image Avarthar tanoshimi · Jan 10, 2017 at 05:22 PM 0
Share

Thx for the help ^^. $$anonymous$$y brother came in and explained exactly why it didn't work. Now it gets everything correctly ^^.

Thx again. You helped me understand what was the mistake in my code. Anyway, gonna get back to learning a bit more before continuing this script I think ^^.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to get a component from an object and add it to another? (Copy components at runtime) 12 Answers

Inheritance vs RequireComponent -1 Answers

Many ways to access other components got me very confused. 1 Answer

C# GetComponent, component not turning off. 2 Answers

Save a Script Component as a variable. 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