greetings,
i have 2 classes or 2 scripts one is a component of an object with represents a gui button and the other is also a component of an object which is a gui progress bar.
i’m simply trying to call the progressbar number in the progressbar class to the gui button class , but i get this error message:
Assets/NewBehaviourScript.cs(19,17): error CS0266: Cannot implicitly convert type `UnityEngine.Component' to `shoot'. An explicit conversion exists (are you missing a cast?)
this the progress bar class:
using UnityEngine;
using System.Collections;
public class shoot : MonoBehaviour {
public float barDisplay=0; //current progress
public Vector2 pos = new Vector2(20,40);
public Vector2 size = new Vector2(60,20);
public Texture2D emptyTex;
public Texture2D fullTex;
public float Seconds = 20;
public float Minutes = 0;
public float count=0;
public GameObject player;
public Transform playerTransform;
Vector3 position ;
public Transform playerTransform1;
Vector3 position1 ;
public int toggle=0;
public int tog=0;
void OnGUI() {
player = GameObject.Find ("Cube");
playerTransform = player.transform;
GUI.BeginGroup(new Rect(pos.x, pos.y, size.x, size.y));
GUI.Box(new Rect(0,0, size.x, size.y), emptyTex);
GUI.BeginGroup(new Rect(0,0, size.x * barDisplay, size.y));
GUI.Box(new Rect(0,0, size.x, size.y), fullTex);
GUI.EndGroup();
GUI.EndGroup();
}
void Update() {
if(Seconds <= 0)
{
Seconds = 20;
if(Minutes >= 1)
{
Minutes--;
}
else
{
Minutes = 0;
Seconds = 0;
}
}
else
{
Seconds -= Time.deltaTime;
count+= Time.deltaTime;
if(count>5)
{
count=0;
}
else
{
barDisplay =(count)*0.2f; // 1/0.5=2 seconds therefore 1/0.1=10 seconds for if condition
}
}
}
public float getbardis()
{
return barDisplay;
}
}
and this is the class i’m trying to call “getbardis” function from , but it seems i can’t call the class from the first place, i’m not sure what’s wrong with my syntax.
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
public shoot sh;
public float bar=0;
void Start () {
sh=gameObject.GetComponent("shoot"); //problem in this line//
}
void OnGUI () {
if (GUI.Button(new Rect(100, 100,100,50), "press me"))
{
bar=sh.getbardis();
Debug.Log(bar);
}
}
}
i have seen the answer of similar questions, but non of them worked with me!!!