Using iTween.MoveTo with GUITextures

I've been trying to use iTween with C# and there always has casting problem.. How can I use iTween.MoveTo() FUnction in C#... iTween is written in C# but there is no example for iTween usage in C#.. What a shame with this?

Note : buttons *is the array of GUITexture..

* *```* *public void CreateMainMenu()* *{* *float startPosX = 0.0f;* *float finalPosX = 0.5f;* *float startPosY = 0.5f;* *float offsetY = 0.1f;* *buttons = new GUITexture[numButtons];* *for (int i = 0; i < numButtons; i++)* *{* _buttons *= Instantiate(GUIButton, new Vector3(0.0f, startPosY, 1.0f), Quaternion.identity) as GUITexture;*_ _*//Animate the Button*_ _*Vector3 finalPos = new Vector3(finalPosX, startPosY, 1.0f);*_ _iTween.MoveTo((GameObject)buttons *, finalPos, 1.0f);*_ _*startPosY -= offsetY;*_ _*}*_ _*}*_ _*```*_ _*

The error for the above code is " Cannot convert GUITexture to UnityEngine.GameObject" .. How can I use iTween in C#??*_ _*anyone helps me...*_ _*please

*_

I've fixed the code formatting and the title of your question. Your problem has nothing to do with casting in C# but with iTween.MoveTo currently not supporting GUITexture or Component (which GUITexture is a descendant of) but only GameObject. You'd run into the same problem in any other language.

4 Answers

4
iTween.MoveTo(...)

only works on GameObjects, you can't use it on GUITexture. But there's good news: Each GUITexture also has a gameObject (because it's a component attached to a game object). You can access the GameObject of GUITexture via the gameObject property.

So, the solution is simply:

iTween.MoveTo(buttons*.gameObject, ...);*
*```*
*<p>... and ... as mentioned in my comment to your question: This has nothing do with C#.</p>*

Glad to hear this fixed it for you.

Just added an example that shows how to animate GUITextures, hope it helps: http://itween.pixelplacement.com/examples.php

Bob, does your script using PixelInset or it's animating with transform ?

Try this:

GameObject button = buttons *as GameObject;*
*if( button != null)*
 *iTween.MoveTo(button, finalPos, 1.0f);*
*else*
 *Debug.Log("Button was null");*
*```*
*<p>EDIT:</p>*
*<p>Upon further investigation, a GUITexture does not inherit from GameObject. Therefore, a GUITexture is not a GameObject, and will return null if you try to cast it as one.</p>*

I know iTween can animate GUI elements, because they have an example on their webpage (that was added yesterday). http://itween.pixelplacement.com/examples.php It costs $2 to buy. My assumption on how it works is that the GUITexture is contained inside an element that iTween can manipulate.

iTween.MoveTo(Your game object name,iTween.Hash(“x”,0.2f,“y”,3,“time”,2.0f,“delay”,2.0f));

if you using this you can move any game object.