Need help connecting these scripts. simple one on one turn based RPG that will feature a sider that will act as a wait time gauge. Gauge works, but I want it to mean that its the monsters turn to act.
keep getting — The name UpgradeSpeed does not exist in this context
and
cannot access a nonstatic member of outer type statmachine via nested type statmachine.speediconslider
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class statmachine : MonoBehaviour {
public Lizard lizard;
public enum TurnState
{
WAIT,
READY,
ACTION,
DEAD,
}
public TurnState currentState;
void start ()
{
currentState = TurnState.WAIT;
}
void update ()
{
switch (currentState)
{
case (TurnState.WAIT):
UpdateSpeed ();
break;
case (TurnState.READY):
break;
case (TurnState.ACTION):
break;
case (TurnState.DEAD):
break;
}
}
public class SpeedIconSlider : MonoBehaviour{
public float speed;
public Slider slider;
public float maxTime;
public KeyCode debugResetKey;
void Start () {
slider.value = 0;
slider.minValue = 0;
slider.maxValue = maxTime;
}
void Update () {
UpdateSpeed();
if (Input.GetKeyDown(debugResetKey)) {
ResetSpeed();
}
}
void ResetSpeed() {
slider.value = 0;
}
void UpdateSpeed() {
if (slider.value < slider.maxValue) {
slider.value += speed * Time.deltaTime;
currentState = TurnState.READY;
}
}
}
}
Ok, so if you want to access your SpeedIconSlider class, you have to have a reference to it.
//Within statmachine
public SpeedIconSlider speedIconSlider; //Drag and drop in the inspector if you can.
speedIconSlider.UpdateSpeed();
The easy way is drag and drop, but you can use other ways of setting up references. But, looking closer and reviewing that error. Do you have a class within a class? Not two different scripts?
Definition 2 scripts. Public class SpeedIconSlider is a copied from another script. Trying to combined these 2 into one script but still learning proper structure for C# (I only have a CS5 background).
If you do not specify a variable/method as private, protected or public, it will the default to private.
This means that UpdateSpeed() is private to the class SpeedIconSlider and can thus be only invoked within that class.
So the solution to your problem is to make UpdateSpeed() public, so you can access it from the parent class statmachine.
I’m also not sure if it is even possible to have MonoBehaviours inside MonoBehaviours. It certainly isn’t needed here.
You could remove the MonoBehaviour from your SpeedIconSlider class and make the Start () and Update() methods of SpeedIconSlider public. Then just call them from your statmachine’s Start() and Update().
Just split it into two scripts. @DaDonik did make another point that your method does need to be public. The other option is to get rid of SpeedIconSlider class and move all the methods and variables and such into your other class, but that may or may not be needed. To split them, you should have two different scripts in two different files.
Also your SpeedIconSlider.UpdateSpeed (); call is incorrect. Always important to look at your variable names.
Your variable is SpeedIconSlide not SpeedIconSlider.