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 TheDarkSeal · Jul 10, 2012 at 03:52 PM · variablecomponentincrease

increase a variable of a component script

Hi I've got a really basic Question: I've got a Script attached to an enemy and i want to increase a variable of an other script when the enemy is dead

 var MaxLife : int = 100;
 var CurrentLife : int;
 var RagDoll : Transform;
 var Enemy : GameObject;
 var Spawnscript : spawnscript;


 function Start () {
 CurrentLife = MaxLife;
 Spawnscript = GetComponent(spawnscript);
 }

 function ApplyDamage(Damage : float){
 if(CurrentLife < 0){
 return;
 }
 
 CurrentLife -= Damage;
 if(CurrentLife < 1){
 var ragdoll = Instantiate(RagDoll, Enemy.transform.position, Quaternion.identity);
 Destroy(gameObject);
 Spawnscript.Score += 1;

 
 
 }
 
 }



 
Comment
Add comment · Show 2
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 fafase · Jul 10, 2012 at 04:04 PM 0
Share

Is it score you want to increase? Is it on another game object?

avatar image Mr.Jwolf · Jul 10, 2012 at 04:19 PM 0
Share

If the Spawnscript is attached to the same gameobject as the script you have posted, you are destroying the instance of the Spawnscript too.

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by TheDavil86 · Jul 10, 2012 at 04:20 PM

You have to first find the object that contains the script. So you could either create a public var of type GameObject and drag the object using the score into it, then you could say for example:

 var object : GameObject;
 
 // Add 1 to the score
 object.GetComponent("Score").score++;

The other solution is to use Find for the game object. So instead you can say:

 GameObject.Find("ScoreObject").GetComponent("Score").score++;
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 fafase · Jul 10, 2012 at 04:35 PM

Now to access, if the script holding the score is on the same object (I doubt it) it won*t do anything but I guess it is on the player.

 var other:SpawnScript;
 
 function Start(){
 other = GameObject.Find("Player").GetComponent(SpawnScript); //I reckon it is on the player
 }
 
 if(CurrentLife < 1){
 var ragdoll = Instantiate(RagDoll, Enemy.transform.position, Quaternion.identity);
 other.Score += 1;
 Destroy(gameObject); //Note: no matter where you place that one, it is called once the update is done.
 }

OR!!!!

in the SpawnScript:

 static var Score:int;

then you access it anywhere else with:

 SpawnScript.Score +=1;

This is fine since you have one socre only in the game.

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 f34rtehninja · Jul 10, 2012 at 04:42 PM

If you want to get a reference to the spawnscript on another gameobject you would need to do

 var EnemyManagerOrSomething = GameObject.Find("ScriptGameObjectNameHere");
 EnemyManagerOrSomething.GetComponent(spawnscript);

If the spawnscript is attached to the Enemy, and you have two scripts attached to the Enemy object than your GetComponent line will work.

Another issue you have is CurrentLife is an int and Damage is a float. Then you do CurrentLife -= Damage, which will throw an error or at least a warning. Current Life could be changed to a float and then just don't display any precision on it when you display it.

The instantiate line should work, putting a RagDoll at the transform.position with rotation of Quaternion.identity which I believe is what you want.

The Destroy(gameObject) line will destroy the object that the script is attached to.

Spawnscript.Score += 1; Will work if the Score is a public variable in the other script, the referencing and syntax works as long as you set up Spawnscript correctly.

Everything else should work. Could you share what particular problems or warnings your having? Is it a compiler error or a behavior error? As in does the console give you red text and you can't play or are the enemies doing weird things in game?

Note: The tendency is to have all members/fields, which are the variable names like maxLife and currentLife, start with a lower case letter then capital letter for all the following new words and then all function and script names start with a capital letter like ApplyDamage (which is correct) and SpawnScript and a capital for new words. You're welcome to do whatever you like, but the general trend is to follow those guidelines. When you try and refactor -> rename in MonoDevelop it doesn't even let you change the names unless they follow those rules, member names start with a lowercase, function names start with an Uppercase.

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 TheDarkSeal · Jul 10, 2012 at 06:55 PM

Thanks for the fast response. My Problem was my score variable in my other script wasnt static and GameObject.Find("ScoreObject"). This is my first Question in Unity Answers and I'm suprised how friendly you people are. Sorry for my bad english I'm German.

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

8 People are following this question.

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

Related Questions

Updating variable on another script. It is not working. 2 Answers

How to get a value from an array within another script. 1 Answer

Increase a variable of an other script 2 Answers

How do I find the name and value for each variable of a script component, using Unity script? 2 Answers

Inspector vs Script: Component best practice? 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