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 /
  • Help Room /
This question was closed Jan 24, 2018 at 03:21 PM by someone721 for the following reason:

I solved the issue.

avatar image
0
Question by someone721 · Jan 23, 2018 at 10:55 PM · game objectstring comparison

Comparing string stored in a game object (Not the object name)

I discovered the answer myself. The reason it wasn't working is because I needed to use:gameObject.GetComponent<ScriptName>.string

Hello, thank you for taking the time to look at my question. I'm still new to Unity & C# and have only just begun learning. I'm working on a game right now; it's my first game and I'm still learning so I don't expect it to be very good, but I'm only making this game to learn.

So anyway on to my question. In one of my scripts I've set a public string and attached that to a game object. Now my string is set on a per object basis.

The Script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class characterStats : MonoBehaviour 
 {
     //Variables
     private statManager expNeeded;
     //strings
     public string characterClass;
     //ints
     public int characterLevel;
     public int characterMaxHealth;
     public int characterCurrentHealth;
     public int characterMP;
     public int characterCurrentMP;
     public int characterExp;
     //Floats
     public float characterAttack;
     public float characterDeffence;
     public float characterMagicAttack;
     public float characterMagicDeffence;
     public float characterSpeed;
 
     // Use this for initialization
     void Start () 
     {
         
     }
     
     // Update is called once per frame
     void Update () 
     {
         if (Input.GetKeyUp(KeyCode.Space)) //This is only here for testing
         {
             characterExp += 10;
         }
 
     }
 }

characterClass is the string I'm trying to compare. In another script I'm trying to compare my characterClass string, with another string, the problem is I can't figure out how.

My other script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class statManager : MonoBehaviour 
 {
     private characterStats stats;
     public int xpNeeded;
     public bool levelUp;
     private string characterType;
 
     void Start ()
     {
         characterType = gameObject.GetComponent<characterClass> ();
     }
 
 
     void Update ()
     {
         if (stats.characterExp >= xpNeeded) 
         {
             levelUp = true;
         }
 
         if (levelUp) 
         {
             if (characterType == "Summoner")
             {
                 summoner();
             }
 
         }
     }
     
     void summoner ()
     {
         stats.characterLevel += 1;
         stats.characterMaxHealth = +5;
         stats.characterCurrentHealth = stats.characterMaxHealth - stats.characterCurrentHealth;
         stats.characterMP += 3;
         stats.characterCurrentMP = stats.characterMP - stats.characterCurrentMP;
         stats.characterAttack += 2;
         stats.characterDeffence += 2;
         stats.characterMagicAttack += 3;
         stats.characterMagicDeffence += 3;
         stats.characterSpeed += 1;
         xpNeeded *= xpNeeded;
         levelUp = false;
 
     }
 }
 

So basically I want to compare my characterClass to a string in my level up check. I hope my explanation was clear enough. Thank you for any help.

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

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

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

Unity getting wrong transform.position from level image when trying to clamp camera within level image 0 Answers

How Can I Stop Particle From Rotating With The Rotating Object It Is Connected To? 0 Answers

How can i move the box collider for a GameObject within a separate cube 0 Answers

My Game mode and Build and run give different results. 0 Answers

Disable Object and Enable by Distance from Player 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