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 CaptainDJ · Aug 10, 2012 at 05:30 PM · callcalling

C# - How do I increase the variable from another script?

Hey, just have a question. On my scr_player, I have it so when you press space you fire a bullet.

I was just wondering how to make it so when that player fires a bullet, a variable from my other script, ShotsFired, increases?

Here is my scr_player:

void Update ()

{

     if (Input.GetKeyDown("space"))

{ //Fire projectile Vector3 position = new Vector3(transform.position.x, transform.position.y + (transform.localScale.y / 2)); Instantiate(ProjectilePrefab, position, Quaternion.identity); } }

Here is my scr_stats:

void Start () { var HP=HPMax; var ShotsFired=0; var Kills=0; }

void Update () { var i=ShotsFired; var ii=Kills;

if (TimeLeft<1) { TotalShotsFired+=i; i=0; TotalKills+=ii; ii=0; TimeLeft=MaxTimeLeft*1.5f; } }

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
1

Answer by Meltdown · Aug 10, 2012 at 05:39 PM

In your other class I'd suggest making a public method that allows you to increase the variable from another script. So do something like..

 int _shotsFired = 0;
 
 public void IncreaseShotsFired()
 {
   _shotsFired++;
 }


Then from the script that calls it.. Get a reference to the above script and just call IncreaseShotsFired();

Comment
Add comment · 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
0

Answer by poncho · Aug 15, 2012 at 06:05 PM

if scr_stats is attached to the same object that your scr_player then use this.getComponent()._shotsFired += 1; and _shotsFired should be public if scr_stats its not in the same gameobject, then find the gameobject that contains it and use the getcomponent method

Comment
Add comment · 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

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

10 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

Related Questions

Script Execution Order removes scripts on apply 1 Answer

Call a Function from another Script 1 Answer

Why can't I call function from another script? 1 Answer

Can I make an array of scripts? 3 Answers

Call inspector editor script function from another editor 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