- Home /
Question by
GingerV1k1ng · Apr 17, 2013 at 12:47 PM ·
c#gui
Why does my slide out menu not work when i translate from JS to C#?
So I am practesing my translation and i decided to make a slide script in JS and it works like it should, so i started with the translation but I am having a problem in the sliding function itself it won't do it. My JS script:
var MyBoxLeft = -200.0;
var icon : Texture; var icon2 : Texture; var blankSkin : GUISkin;
function OnGUI () { GUI.skin = blankSkin; var tBoxRect = new Rect(MyBoxLeft, 20.0, 220, 720); var tButtonRect = new Rect(MyBoxLeft, 160.0, 120, 30); var tBoxArt = new Rect(MyBoxLeft, 200.0, 120, 120);
GUI.Box(tBoxRect, icon); GUI.Button(tButtonRect, "Click"); GUI.Box(tBoxArt, icon2); }
function Update () {
if (Input.GetKeyDown("space")) { AnimateBox(); }
}
function AnimateBox () {
if (MyBoxLeft == -200.0) {
while (MyBoxLeft < 20.0) {
MyBoxLeft+=5.0;
yield;
}
} else if (MyBoxLeft == 20.0) {
while (MyBoxLeft > -200.0) {
MyBoxLeft-=5.0;
yield;
}
}
}
and here is the C# script i have made:
using UnityEngine;
using System.Collections;
public class GUI_Script : MonoBehaviour
{
float MyBoxLeft = -200f;
public Texture icon;
public Texture icon2;
public GUISkin blankSkin;
void OnGUI()
{
GUI.skin = blankSkin;
Rect tBoxRect = new Rect(MyBoxLeft, 20.0f, 220, 720);
Rect tButtonRect = new Rect(MyBoxLeft, 160.0f, 120, 30);
Rect tBoxArt = new Rect(MyBoxLeft, 200.0f, 120, 120);
GUI.Box(tBoxRect, icon); GUI.Button(tButtonRect, "Click"); GUI.Box(tBoxArt, icon2);
}
void Update()
{
if (Input.GetKeyDown("space")) { AnimateBox(); }
}
void AnimateBox()
{
if (MyBoxLeft == -200.0f)
{
while (MyBoxLeft < 20.0f)
{
MyBoxLeft += 5.0f;
yield return 0;
}
}
else if (MyBoxLeft == 20.0f)
{
while (MyBoxLeft > -200.0f)
{
MyBoxLeft -= 5.0f;
yield return 0;
}
}
}
}
I know that I need to make the void AnimateBox() to a IEnumerator for it to say it works, but it will not scroll.
Comment
Best Answer
Answer by whydoidoit · Apr 17, 2013 at 12:48 PM
You need to make AnimateBox an IEnumerator and call it with StartCoroutine(AnimateBox());
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
GuiTexture Width Change 1 Answer
JS to C# Translation? 2 Answers