I have been a silent reader of these forums until now. As I really need your help!
For my game I am using an asset called Realistic FPS Prefab, i’m sure some of you are aware of this. The reason i’m not asking them for help as i’m customising it, of which should be simple to you guys.
In game
Player kills zombie, i want 10 xp to be added to the levelling script system attached to FPSPlayer
This is the code for levelling.cs which is assigned to the FPSPlayer object
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class levelling : MonoBehaviour {
public int xp;
public int level;
// Use this for initialization
void Start () {
xp = 0;
level = 1;
}
// Update is called once per frame
void Update () {
}
}
And this is the quote assigned in CharacterDamage.cs within the AI.
public class CharacterDamage : MonoBehaviour {
public GameObject player;
}
void Die() {
Levelling = player.GetComponent<levelling>();
Levelling.xp + 10;
}
The error i am getting is:
Assets/RFPSP/Scripts/AI/CharacterDamage.cs(254,3): error CS0103: The name `Levelling’ does not exist in the current context
and
Assets/RFPSP/Scripts/AI/CharacterDamage.cs(256,13): error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement
In CharacterDamage you have a brace out of place (and possibly one missing at the end, unless you didn’t paste it in). Under “GameObject player” there shouldn’t be a closing brace there. So, remove that & add one at the end of the file, to match the class opening one, if it’s not already there.
Next, the code in the same file should be:
levelling lev = player.GetComponent<levelling>(); // syntax is 'type variable_name' on the left.
lev.xp += 10; // adding 10 plus assigning to the variable.
Another option is to have a method on the “levelling” script which you can call from the other, which takes an int parameter for how much xp to add. That is a little nicer, but since you are pretty new, either I think is okay for now. =)
One convention, though you don’t have to use, but I find this one particular good, is to switch the name “levelling” to “Levelling”, with a capital at the beginning.
Regarding the closing bracket, i just pasted the segments wrong.
What you suggested has stopped giving me errors (thank god… “you”) however when I kill the zombie (Which the CharacterDamage script is assigned to) the xp value of 10 isnt added to the public xp variable within levelling.cs attached to the player object.
To avoid any confusion i will show the full code of the CharacterDamage script as i didnt do a good job last time showing you what you needed, I hope its not too long. (Head down to the die() function which is where the code you suggested is on line 252.
Ok, that is truly strange. When you say if you change the name it gives you an error, is that a error before you run?
If you put any Debug.Log even at the top of the Die() Method, like Debug.Log (“Hello Unity!!”); does that even print for you?
I mean, the code we talked about is right near the top… if those variables aren’t null, then something is weird (or the method isn’t even running?)… Sorry, let me know how that goes
So, it didnt work in the function die()… so i just put it further up the code before the function is called, with the if statement for when health hits zero basically.
Methos is now going to be a character in Dawn of Whiteland, before without the mighty Methos, noone would be able to level up