- Home /
How do I access this value properly so my characters exp goes up?
Hey I've been trying to get a level up system for skills and I think I'm pretty close to having it setup how I want it. However I am having trouble figuring out how to have my character gain Exp when he chops the tree (Accessing the woodcuttingcurExp from the first script and changing it so it adds 25exp when cutting the tree).
I got 1 Script that is on my character that takes care of his Experience & Leveling system, this is working fine but I posted this so it'd be easier to help me with the problem since I have to access woodcuttingcurExp from here to add 25exp.
THIS SCRIPT IS ATTACHED TO MY PLAYER
Skills2.cs
using UnityEngine;
using System.Collections;
public class Skills2 : MonoBehaviour {
public int woodcuttingCurrentLevel = 1;
public int woodcuttingMaxLevel = 99;
public int woodcuttingcurExp = 1;
public int woodcuttingmaxExp = 100;
public float expBarLength;
void Start () {
expBarLength = Screen.width / 2;
}
void Update () {
AdjustCurrentExp(0);
if(woodcuttingcurExp >= woodcuttingmaxExp)
{
woodcuttingcurExp -= woodcuttingmaxExp;
woodcuttingCurrentLevel++;
woodcuttingmaxExp += (20 * woodcuttingCurrentLevel);
}
}
void OnGUI()
{
GUI.Box(new Rect(20, 50,
//expBarLength "Delete the 200 and put this back in to get moving exp bar
200, 20), woodcuttingcurExp + " / " + woodcuttingmaxExp);
GUI.Box(new Rect(20, 90, 200, 20), "Level: " + woodcuttingCurrentLevel + " / " + woodcuttingMaxLevel);
}
public void AdjustCurrentExp(int adjExp)
{
woodcuttingcurExp += adjExp;
expBarLength = (Screen.width / 3) * (woodcuttingcurExp / (float) woodcuttingmaxExp);
}
}
This is the 2nd script that I have on my object (tree) that allows me to cut it down, the problem I am having is I can't figure out how to reference the character's Skills2 Script and get it to add Experience to my characters woodcuttingcurExp.
Here is where I am having the problem, Unity 3D Error says "Unknown identifier skills2". I have tried quite a few ways to access and change that value but so far haven't been able to change it. I have tried to access it with the following 2 lines of code as well but haven't been able to figure out exactly what to type in here.
var player = GameObject.Find("Player");
// var skills2: Skills2 = GetComponent(Skills2);
(I get an error when this code above is put in because it's probably typed in wrong that is why it's edited out with the 2 // before it)
if(chopping){
**skills2.woodcuttingcurExp += 25;**
(This line above is the main problem, I can't figure out how to access and change the woodcuttingcurExp value properly)
Here is the entire 2nd Script where I am having the problem.
THIS SCRIPT IS ATTACHED TO THE TREE OBJECT THAT I WANT TO GAIN EXP FROM WHEN CHOPPED
NormalTree.js
var duration : float = 5;
var stumpPrefab : GameObject;
var stackPrefab : GameObject;
private var startTime : float = 0;
private var showGUI : boolean = false;
private var chopping : boolean = false;
var treeWoodcuttingLevel : int = 1;
var XPGainedAfterCuttingDown : int = 25;
function OnMouseOver () {
var player = GameObject.Find("Player");
// var skills2: Skills2 = GetComponent(Skills2);
// if(Vector3.Distance(GameObject.Find("Player").transform.position, transform.position) > 2.0)
// return;
showGUI = true;
if(Input.GetMouseButtonDown(0)){
chopping = true;
startTime=Time.time;
}
if(chopping){
skills2.woodcuttingcurExp += 25;
// player.animation.CrossFade("1h_attack1");
if(Time.time - startTime < duration){
Instantiate(stumpPrefab, transform.position, transform.rotation);
Instantiate(stackPrefab, transform.position, transform.rotation);
}
else{
chopping = false;
Destroy(gameObject);
}
}
}
function OnMouseExit () {
showGUI = false;
}
function OnGUI () {
if(showGUI && !chopping)
GUI.Label (Rect (10, 10, 100, 20), "Chop down this tree");
}
If anyone could please show me how to properly access and change this value so my character gains experience when cutting the tree it would be much appreciated, thanks.
Answer by ryba · Jan 23, 2013 at 08:06 AM
You must call GetComponent on particular game object (but with another component works as well):
var skills2: Skills2 = player.GetComponent(Skills2);
And add this line where u want to increase experiance:
skills2.woodcuttingcurExp += XPGainedAfterCuttingDown;
Thanks I'm pretty sure I got it setup exactly how it should be however it's still not able to access it for some reason. I've setup a brand new project and put in only the lines of code necessary for it to work but it gives me a Null Reference Exception like it can't access the Skills Script on the "Player" object.
Things I have done is rewrite the c# script to Java so that both scripts are Javascript now. Both Scripts are placed in the same folder.
I create a brand new Project and import these 2 Scripts.
Skills.js (Created a cube, set it's tag to "Player", then attached this script to it, so that the next script should be able to find this object with a "player" tag, with a Component Script (Skills.js) attached to it with the variable woodcuttingcurExp to add 25 exp to when I click on the NormalTree Object)
#pragma strict
public var woodcuttingcurExp : int = 1;
function Start () {
}
function Update () {
}
NormalTree.js (Created a 2nd cube and attached this script to it, this script seems to find the object with the "Player" tag properly when clicked, however it cannot seem to find the Skills Script which is attached to it.)
#pragma strict
function On$$anonymous$$ouseOver () {
if(Input.Get$$anonymous$$ouseButtonDown(0)){
var player = GameObject.Find("Player");
var skills: Skills = player.GetComponent(Skills);
// var skills: Skills = GameObject.Find("Player").GetComponent(Skills);
skills.woodcuttingcurExp += 50;
}
}
Then I start the project up to test and when I go to click the NormalTree Object ins$$anonymous$$d of accessing the Skills.js Script and changing the value it give me an error saying Null Reference Exception pointing at this line:
var skills: Skills = player.GetComponent(Skills);
I have tried rewording parts of the code slightly, making new scripts with just these lines of code so it should have been easier to spot the problem, I changed the script names and tried referencing them differently.
Your answer
![](https://koobas.hobune.stream/wayback/20220613091440im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Find GameObjects with a true boolean and put them in an array? 1 Answer
Need help with in game custom clock! 0 Answers
why wont this SendMessage work 1 Answer
How to get the value of a boolean in game object to another Game object script. 1 Answer
Performance issues with GetComponent/gameObject.Find in different functions 1 Answer