So I have a random number generator script and i’m trying to access one of its functions from another game object with a script attached to it.
The object that has my RNG.cs attached to it is called Roller. Here is the RNG.cs
using UnityEngine;
using System.Collections;
public class RNG : MonoBehaviour {
private int rngD4 = 4;
private int rngD6 = 6;
private int rngD8 = 8;
private int rngD10 = 10;
private int rngD12 = 12;
private int rngD20 = 20;
private int rngD100 = 100;
private int theRoll;
public void D4(){
int theRoll = Random.Range(1, rngD4);
}
public void D6(){
int theRoll = Random.Range(1, rngD6);
}
public void D8(){
int theRoll = Random.Range(1, rngD8);
}
public void D10(){
int theRoll = Random.Range(1, rngD10);
}
public void D12(){
int theRoll = Random.Range(1, rngD12);
}
public void D20(){
int theRoll = Random.Range(1, rngD20);
}
public void D100(){
int theRoll = Random.Range(1, rngD100);
}
}
What I’m having a problem with is how to simply pass ->theRoll<- (which should be the number generated) to WeaponDamage in my script attached to the weapon.
using UnityEngine;
using System.Collections;
public class LongSword : MonoBehaviour {
public GameObject Roller;
private string WeaponName = "Long Sword";
public int WeaponDelay = 3;
public int WeaponRange = 3;
public static int WeaponDamage;
void Start()
{
WeaponDamage = Roller.GetComponent<RNG>().D8; //rolling of the specific damage dice from RNG.cs
}
}
The specific error I’m getting is:
Cannot convert method group D8' to non-delegate type int’. Consider using parentheses to invoke the method
I THINK I’m aware of what the problem is, but I’m having a hard time getting my head around it. The other thing I’m concerned with - is when I invoke my function “Attack” in another script, that it will pass the same int over and over again, as opposed to getting a new roll every time. (Maybe not - but its a concern)
The attack function (I’ve not passed the variable yet to the script (its currently -10)
private void Attack(){
float distance = Vector3.Distance (target.transform.position, transform.position);
Vector3 dir = (target.transform.position - transform.position).normalized;
float direction = Vector3.Dot (dir, transform.forward);
Debug.Log(direction);
if (distance < 2.5f) {
if(direction > 0) {
EnemyHealth eh = (EnemyHealth)target.GetComponent ("EnemyHealth");
eh.AdjustCurrentHealth (-10);
}
Can anyone give me a hand with this? I’d certainly appreciate it. Getting a handle on this would certainly help me in many respects, considering I’m going to be working an init roll, delay and range into my attack script as well.