Constant compile errors calling functions from another class.

I’m relatively new to C#, but not an entire newblet. What I’m trying to create is a simple die rolling class (in the d20 style) with a separate function for each die, that can be called from other scripts (to save doing the math/Random code each time I need to roll one of the die). That also stores the result in a variable, (So it can be checked if the same result is needed again) but can also be re-rolled when required. I’ve tried it a few separate ways, but no matter how, every time I attempt to call them from other scripts, I get numerous and incessant compile errors. I’m certain I’m missing just one simple thing, and I’ll probably boot myself up the rear when I realize what I’ve forgotten to add! Anyway, here is what I have tried so far;

using UnityEngine;
using System.Collections;

public class DieRoller : MonoBehaviour {

public static DieRoller instance2;

public int d4 = 0;
public int d6 = 0;
public int d8 = 0;
public int d10 = 0;
public int d12 = 0;
public int d20 = 0;
public int d100 = 0;
public bool rolld20 = false;

void Awake ()
{
instance2 = this;	
}

void Start () {

}

void Update () {

}

public void rolld4()
{
d4 = Random.Range (1,4);
}

public void rolld6()
{
d6 = Random.Range(1,6);
}

public void rolld8()
{
d8 = Random.Range(1,8);
}

public void rolld10()
{
d10 = Random.Range (1,10);
}

public void rolld12()
{
d12 = Random.Range(1,12);
}

public int rollDie(bool rolld20)
{
	if (rolld20 == true)
	{
	d20 = Random.Range(1,12);		
	}
}


public void rolld100()
{
d100 = Random.Range(1,100);
}

}

The various methods I’ve attempted of calling the functions are also meeting the same fate. What have I missed? Please, oh wise Unity gurus, save me from the constant head-desking over such an easy problem, before I give myself a concussion! :stuck_out_tongue:

That class looks fine, how are you calling the functions? Could you show that script?

What direction do you need? The library comes with documentation and examples already: // Extracting features from two fingerprint images var featExtractor = new MTripletsExtractor(){ MtiaExtractor = new Ratha1995MinutiaeExtractor()}; var features1 = featExtractor.ExtractFeatures(fingerprintImg1); var features2 = featExtractor.ExtractFeatures(fingerprintImg2); // Match features var matcher = new M3gl(); double similarity = matcher.Match(features1, features2); if similarity exceeds a given threshold, the fingerprints match.

1 Answer

1

Well your rollDie function is going to throw an error But I can’t quite tell what it is supposed to do, so I will let you fix that one.

Assuming you have this script attached to a game object called diceBag there are a few ways you can call the functions and access the results but this is the way I would recommend:

public DieRoller dice;

// Use this for initialization
void Start () {
	//we find the GameObject called "diceBag" and we look on it for a DieRoller component
	dice = GameObject.Find("diceBag").GetComponent<DieRoller>();

	//then to roll a die 
	dice.rolld6();

	//and to access the results
	Debug.Log(dice.d6);
}

Also, after looking at your code you should check the reference for Random.Range

The int version of that function is exclusive on the top end so your rolld12() function, as you currently have it written, it will roll numbers ranging from 1 to 11. You are probably going to want it to look like this:

public void rolld12()
{
	d12 = Random.Range(1,13);
}