Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Conect11 · Jan 31, 2018 at 04:58 AM · stringclass

Adding from one script to another with a String Name

Bear with me here. I'm sorry if this has been asked before, I just didn't know how to phrase this in search. Does anyone know how I could add an integer from one script into another without actually adding directly from a variable? I know THIS Works: (SCRIPT 1) Playerhealth has a class called "hp" So in Script 2 I could say

 PlayerhealthScript.hp += 1;

But let's say I want that to be dynamic. Let's say that I want a space in the inspector of Script 2 to write in which class in Playerhealth will have that 1 added. Something like: (Script 2)

 var chooseaclass : String;
 PlayerhealthScript.chooseaclass +=1;

Now, I know that isn't the correct syntax (already tried) but is something like this even possible? If so, what is the correct syntax? 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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by BloodMarked · Jan 31, 2018 at 10:29 AM

i think a pointer is what you are looking for. dont know enough about using them though. pointers are pretty advanced stuff.

something like this is usually solved with a function in the playerHealthScript like p.e.:

     var health1, health2 = 100;
     function changeHealth(var change){
       if (condition) {
         health1 += change;
       } else {
         health2 += change;
       }
     }

,then call PlayerHealthScript.changeHealth(-4); in the other script

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 KittenSnipes · Feb 01, 2018 at 06:00 AM

@Conect11

Well I think you just need public functions and private variables for each and every variable so you can easily change them. Oh here is a giant example script to emphasize what I mean. It is in UnityScript which I am not too fluent in but I gave it my best shot. Cheers! :

     #pragma strict
     import UnityEngine;
         //Max Amounts
         var maxCancerCells: int = 100;
         var maxDiabetesCells: int = 100;
         var maxFluCells: int = 100;
         var maxHealth: int = 100;
         var maxInsanity: int = 100;
         var maxPanic: int = 100;
         var maxResourcefulness: int = 100;
     
         //Current Stats
         private var cancerCellCount: int;
         private diabetesCellCount: int;
         private fluCellCount: int;
         private currentHealth: int;
         private insanityAmount: int;
         private panicAmount: int;
         private resourcefulnessAmount: int;
     
         //Current Statuses
         var cancer: boolean;
         var diabetes: boolean;
         var dead: boolean;
         var flu: boolean;
         var insane: boolean;
         var panicked: boolean;
     
         function Start() {
             this.currentHealth = this.maxHealth;
             this.insanityAmount = 0;
             this.panicAmount = 0;
             this.resourcefulnessAmount = 0;
             this.cancer = false;
             this.diabetes = false;
             this.dead = false;
             this.flu = false;
             this.insane = false;
             this.panicked = false;
         }
     
         function Update(){
             if (this.cancerCellCount >= this.maxCancerCells) {
                 this.cancer = true;
             }
             if (this.currentHealth <= 0) {
                 this.dead = true;
             }
             if (this.diabetesCellCount >= this.maxDiabetesCells) {
                 this.diabetes = true;
             }
             if (this.fluCellCount >= this.maxFluCells) {
                 this.flu = true;
             }
             if (this.insanityAmount >= this.maxInsanity) {
                 this.insane = true;
             }
             if (this.panicAmount >= this.maxPanic) {
                 this.panicked = true;
             }
         }
     
         public GetCancerCellCount() : int {
             return this.cancerCellCount;
         }
     
         public function SetCancerCellAmount(cancerCells: int) {
             this.cancerCellCount = cancerCells;
         }
     
         public GetDiabetesCellCount() : int{
             return this.diabetesCellCount;
         }
     
         public function SetDiabetesCellAmount(diabetesCells: int){
             this.diabetesCellCount = diabetesCells;
         }
     
         public GetFluCellCount(): int {
             return this.fluCellCount;
         }
     
         public function SetFluCellAmount(fluCells: int){
             this.fluCellCount = fluCells;
         }
     
         public GetHealth(): int {
             return this.currentHealth;
         }
     
         public function SetHealth(healthToSetAs: int){
             this.currentHealth = healthToSetAs;
         }
     
         public AddHealth(healthToAdd: int){
             this.currentHealth += healthToAdd;
         }
     
         public GetInsanityAmount(): int {
             return this.insanityAmount;
         }
     
         public function SetInsanityAmount(insanityToSetAs: int){
             this.insanityAmount = insanityToSetAs;
         }
     
         public function AddInsanityAmount(insanityToAdd: int){
             this.insanityAmount += insanityToAdd;
         }
     
         public GetPanicAmount(): int {
             return this.panicAmount;
         }
     
         public function SetPanicAmount(panicToSetAs: int){
             this.panicAmount = panicToSetAs;
         }
     
         public function AddPanicAmount(panicToAdd: int){
             this.panicAmount += panicToAdd;
         }
     
         public HasCancer(): boolean {
             return this.cancer;
         }
     
         public HasDiabetes(): boolean {
             return this.diabetes;
         }
     
         public HasTheFlu(): boolean {
             return this.flu;
         }
     
         public function SetCancer(checkForCancer: boolean){
             this.cancer = checkForCancer;
         }
     
         public function SetDiabetes(checkForDiabetes: boolean){
             this.diabetes = checkForDiabetes;
         }
     
         public function SetTheFlu(checkForFlu: boolean){
             this.flu = checkForFlu;
         }

You would then use the script like this:

 Playerhealthscript.AddInsanityAmount(1);


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 Bonfire-Boy · Jan 31, 2018 at 04:38 PM

There are many ways to achieve the kind of thing you're after, and the best solution is going to depend on the details of what you're trying to achieve.

But one simple solution that gives you exactly what you're asking for, would be to use a Dictionary. By "what you're asking for" I am talking not only about the fact that you want to be able to specify the variable to modify by using a string, but also the implied fact that all those variables are integers.

In your Playerhealth class you would replace the int hp; field with something like

 Dictionary <string, int> namedInts = new Dictionary< string, int >() { { "name0", 0 }, { "name1", 1 } };

You would then be able to access and use the ints with

 string intToModify = "name1"; // or whatever
 PlayerhealthScript.namedInts[intToModify] += 1; // and so on 

This would be totally dynamic (eg you'd be able to add new "namedInts" to a Playerhealth object at runtime)

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

76 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 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 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 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 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

How to access var in other class using string 0 Answers

CSharp Classes = Scripts? Difference between private string and normal string? 1 Answer

How do i convert a String into a Class reference in C# 1 Answer

Show variable name from a class as String 2 Answers

GUI set max amount of characters for Label 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