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 pickle chips · Jun 24, 2013 at 11:33 PM · variablesweaponpropertiesequip

Issues with accessing properties in c#

I'm trying to implement an item system, And I want to change the values of certain variables in a bunch of scripts depending on the weapon the player has equipped.

I have a class, "Weapon", with properties of MaxDamage,AttackSpeed, and Range. (This class inherits from an Item class I made)

In another script, "PlayerCharacter", I have a property called EquipedWeapon, of type Weapon. Right now, since I don't have the equipment system set up yet, i just assisgned the variable EquipedWeapon references as a new Weapon(); using the default constructor.

In my attack script for the player, I'm trying to set the maxDamage in that script to the MaxDamage of the EquipedWeapon property, but I can't seem to get it to work properly.. I'm getting the Error: Object reference not set to an instance of an object. Does anyone know what's wrong?

Heres the code:

Assigning the equippedWeapon public class PlayerCharacter : MonoBehaviour {

     public Weapon equipedWeapon;
     
     private Weapon EquipedWeapon{
         get { return equipedWeapon; }
         set { equipedWeapon = value; }
     }
     
     // Use this for initialization
     void Start () {
         equipedWeapon = new Weapon();
     }

And heres the attack script, Trying to reference the properties of the EquipedWeapon property:

 public void adjustVars() {
             PlayerCharacter pc = (PlayerCharacter)GetComponent("PlayerCharacter");
     
             maxDamage = pc.EquipedWeapon.MaxDamage;
             attackSpeed = pc.EquipedWeapon.AttackSpeed;
             range = pc.EquipedWeapon.Range;
         }

Does this make sense? Can anyone tell me what I'm doing wrong?

-Thanks

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

Answer by Kaivo · Jun 24, 2013 at 11:44 PM

Could it be that the GetComponent is not from the instanciated object? You should get a reference of the object on which the "PlayerCharacter" script is attached and perform the GetComponent from this object's reference. Maybe something like :

 GameObject.FindWithTag("PlayerCharacter").GetComponent("PlayerCharacter");

or something along those lines. Also, instead of putting this in the fuction, make it a private variable of your class and call this line on Start(), so that you have to find it only one time.

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 pickle chips · Jun 25, 2013 at 01:14 AM 0
Share

Oh god I'm sorry! I feel quite stupid, It was just because I didn't attach the script to the player Character! I did at one point, but I closed unity without saving the scene..

Your answer made me realize this, so I will accept it!

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

16 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

Related Questions

Are properties variables? 1 Answer

Why is it that when I equip weapons, they rotate in different ways depending on which way I[my character-player] am[is] looking? 1 Answer

Accessing class properties through getters & setters in editor mode 1 Answer

Character Equip Animation 0 Answers

How to get a List of all Variables in a Class 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