I solved the issue.
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.