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 Tyrrell G · Jan 23, 2013 at 06:57 AM · getcomponentgameobject.findexperience

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.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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;
Comment
Add comment · Show 1 · 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 Tyrrell G · Jan 23, 2013 at 11:06 PM 0
Share

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

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

10 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

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


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