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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by aidengaming123 · Feb 11, 2015 at 09:37 AM · referencenon-static

Referencing non-static variable from script on child C#

I'm trying to reference a non-static varaible from a script, WeaponScript. WeaponScript is found in several child objects which are toggled on and off, and each contain a variable machineGun.bulletsLeft (I want to just change the enabled object's variable). Here's what I have so far:

 using UnityEngine;
 using System.Collections;
 
 public class ammopickup : MonoBehaviour {
     int ammo;
     Component script;
 
     void Awake () { 
         script = GameObject.FindWithTag("Player").GetComponentInChildren<WeaponScript>();
         ammo = script.machineGun.bulletsLeft;
         
     }
     
     void OnTriggerEnter(Collider other){
         ammo = ammo + 20;
         audio.Play ();
     }
 }

This is the error I get: WeaponSystem/ammopickup.cs(11,31): error CS1061: Type UnityEngine.Component' does not contain a definition for machineGun' and no extension method machineGun' of type UnityEngine.Component' could be found (are you missing a using directive or an assembly reference?)

How should I go about fixing this? Is it because the script can't find the script to access the variable, or because I'm stupid and wrote the whole thing wrong. Thanks!

Comment
Add comment · Show 1
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 gjf · Feb 11, 2015 at 09:06 AM 0
Share
 script = GameObject.FindWithTag("Player").GetComponentInChildren<WeaponScript>();

will get any WeaponScript on "Player".

if you're trying to get a specific weapon then you'll need to be more specific in how you get it!

why not have your machine gun inherit from WeaponScript to allow you to have common functionality, but access machine gun specific variables/functionality where required?

that way you can find $$anonymous$$achineGunScript (to use your nomenclature) ins$$anonymous$$d of any weapon.

1 Reply

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

Answer by Baste · Feb 11, 2015 at 09:44 AM

You have declared script like this:

 Component script;

Meaning that you only have access to the variables and methods from the Component class. Now, it's still a WeaponScript, but the compiler doesn't know this. When you do this:

 script = GameObject.FindWithTag("Player").GetComponentInChildren<WeaponScript>();

It works, because your WeaponScript is also a component (because it inherits from MonoBehaviour, which inherits from Behaviour, which inherits from Component), but on the next line, the compiler just sees that you're trying to call .machinegun on a component.

To fix this, just declare the script variable as a WeaponScript:

 WeaponScript script;
 
 void Awake () { 
     script = GameObject.FindWithTag("Player").GetComponentInChildren<WeaponScript>();
     ammo = script.machineGun.bulletsLeft;
 }

Note that "script" is a horrible variable name, since everything is a script.

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 aidengaming123 · Feb 11, 2015 at 09:44 PM 0
Share

That did the job! Thanks (and I know script is a band variable name, I just did it for the question haha)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

An object reference is required to access non-static member 1 Answer

Get variable from seperate script. 1 Answer

reference is required to access non-static member `CarController.Update()' 1 Answer

An object reference is required to access non-static member 4 Answers

Reference creator of gameObject 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