• 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
-1
Question by ChrPunx · Oct 21, 2014 at 01:23 PM · c#valueget-value

reading a value using GetComponent

I'm trying to read a int from a object using 2D colliders, and then apply a specific action if that int is equal to a specific number, unfortunately, not working...

     if (col.collider.gameObject.GetComponent<PlayerControllerHeli>().curPWeapon == 1) {
         col.collider.gameObject.GetComponent<PlayerControllerHeli>().gun.FireRateManager(+value);
     }


any suggestions?

Comment
Add comment · Show 4
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 HarshadK · Oct 21, 2014 at 01:32 PM 1
Share

You need to clearly state the behavior you expected and the output you are getting.

Are you getting any error? Also provide the other script that you are accessing to set the value.

avatar image KayelGee · Oct 21, 2014 at 01:33 PM 0
Share

What do you mean by not working? Is it throwing errors? Is it not behaving as expected? Is curPWeapon never 1? Is col never the object you want?

avatar image ZeusAllMighty11 · Oct 21, 2014 at 01:34 PM 0
Share

What is your second line doing? grabbing the gun's FireRate$$anonymous$$anager, and using +value as a paremter? That's not a thing in C#.

Why not set 'value' as a variable in the FireRate$$anonymous$$anager class, and do:

 col.collider.gameObject.GetComponent<PlayerControllerHeli>().gun.FireRate$$anonymous$$anager.someVariable += value; // or = value or such
avatar image ChrPunx · Oct 21, 2014 at 09:57 PM 0
Share

I'm really just trying to read a int and then apply a specific action if that int is equal to "X".

It doesn't give errors, it just doesn't work, after checking which gun is active(checking curPWeapon == 1 or 2 or 3),it adds a value(float) for the fire rate of that specific gun.

After checking and adding, the object should destroy itself, but apparently it is unable to check it, so it does not add any value, and does not destroy itself.

I know that the problem is with that specific line because it was the part I modified just before it stopped working.

so I hoped, someone would give a solution for the "().curPWeapon == X" problem

1 Reply

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

Answer by Chris_Dlala · Oct 21, 2014 at 02:37 PM

This isn't so much an answer to your question as an answer to the question's title. As others have said, it isn't clear exactly what you want to do? I just want to throw in my 2 pence on the matter. You should try not to get a component each time you want it, and instead get it once and reuse that. It's good practice to check if the component was successfully found before using it. After these two steps you can do as you please with the component as shown below. I'm assuming curPWeapon is an int.

     // Get the component only once
     PlayerControllerHeli temp = col.GetComponent<PlayerControllerHeli>();
 
     // Check we have the component we need
     if(temp != null)
     {
         // Access anything in "temp" as type PlayerControllerHeli
         if (temp.curPWeapon == 1)
         {
             // Do any action you like
             temp.gun.FireRateManager();
         }
     }

You'll notice I took out the +value parameter as I have no idea what you were trying to do with that. I hope this helps a little =D

Comment
Add comment · Show 6 · 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 ChrPunx · Oct 21, 2014 at 09:57 PM 0
Share

I'm really just trying to read a int and then apply a specific action if that int is equal to "X".

It doesn't give errors, it just doesn't work, after checking which gun is active(checking curPWeapon == 1 or 2 or 3),it adds a value(float) for the fire rate of that specific gun.

After checking and adding, the object should destroy itself, but apparently it is unable to check it, so it does not add any value, and does not destroy itself.

I know that the problem is with that specific line because it was the part I modified just before it stopped working.

so I hoped, someone would give a solution for the "().curPWeapon == X" problem

avatar image ChrPunx · Oct 21, 2014 at 10:02 PM 0
Share

and as the value of curPWeapon is not constant, I really need to check just when the collision happens.

This is my project: https://www.youtube.com/watch?v=UuaDr$$anonymous$$0SFys

(the function tested in this video shows how the value of curPWeapon changes, I'm using a single "big" code to handle all collectables, so hoped to keep that part simple)

avatar image ChrPunx · Oct 21, 2014 at 11:13 PM 0
Share

never$$anonymous$$d, the problem was, I was checking if the object had all scripts...So, I've added another question for the If statement, and is working fine now.

     if (GameObject.FindGameObjectWithTag("PlayerSpawner").GetComponent<PlayerController>().vehicleType == VehicleType.Helicopter && col.collider.gameObject.GetComponent<PlayerControllerHeli>().curPWeapon == 1) {
         col.collider.gameObject.GetComponent<PlayerControllerHeli>().gun.FireRate$$anonymous$$anager(+value);
     }
avatar image Chris_Dlala · Oct 22, 2014 at 08:03 AM 0
Share

Ah I'm glad you've solved your problem! However, you could still benefit from the points I made in my answer, you get the same component more than once.

avatar image KayelGee · Oct 22, 2014 at 08:36 AM 0
Share

Also this makes the code much more readable.

@ChrPunx Add an answer and accept it then.

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to assign random int values to gameobjects 2 Answers

Have a problems with a values 1 Answer

How to get float value from other 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