Plott
October 24, 2014, 3:11am
1
Im super lost in trying to figure out how to do this correctly, I have written out i THINK is what is close to the correct code, but I’m lost now.
public Sprite[] array;
int imageNumber = 0;
void Start ()
{
GetComponent<SpriteRenderer> ().sprite = array [49];
}
public void BatteryCollected (Vector2 contactPoint)
{
SpriteRenderer = array[imageNumber];
if(imageNumber == array.Count - 1)
{
imageNumber = 0;
}
else
{
imageNumber++;
}
}
So its not really working at all, but what i want to do is:
If you collect a battery in my game, then the image of battery power on screen will move in positive order of sprites array.
if you move the player it goes in negative order of the sprite array.
Any idea of what I’m writing wrong? I’m confused at where to with this, especially since array.Count doesn’t work.
1 Like
array is a type of Sprite[ ] which is not a List. So to get the number of elements you have to use array.Length
1 Like
Plott
October 25, 2014, 1:31am
3
Ok so I tried to get this to work, but its not moving changing the sprite sequence. I think my issue is on this code
void Start()
{
GetComponent<SpriteRenderer> ().sprite = array[25];
}
I only put 25 there to prove that is was moving to that number in the sequence when clicked and it is. But it needs to be something different. array.Length didn’t work there, or at least it would except it, just gave me an error.
Here is the code i changed. I could really use some knowledge on what to do differently, I have a hard time deciphering Unity Documentation
this is where i add or subtracted from a different script.
public void BatteryCollected (Vector2 contactPoint)
{
Debug.Log ("Battery Plus");
batteryG.BatteryPlus();
}
public void BatteryMinus ()
{
Debug.Log ("Battery Minus");
batteryG.BatteryMinus();
}
So then they talk to this script
public class batteryGUI : MonoBehaviour {
public Sprite[] array;
int imageNumber = 0;
void Start()
{
GetComponent<SpriteRenderer> ().sprite = array[25];
}
public void BatteryPlus()
{
if(imageNumber == array.Length - 1)
{
imageNumber = 0;
}
else
{
imageNumber++;
}
}
public void BatteryMinus()
{
if(imageNumber == 0)
{
imageNumber = array.Length - 1;
}
else
{
imageNumber--;
}
}
}
1 Like
The fact is that you’re substracting your imageNumber, but you’re not reading your array with it. Add this at the end of your BatteryMinus and BatteryPlus func:
GetComponent().sprite= array[imageNumber];
1 Like
Plott
October 25, 2014, 11:33pm
5
Diablo404:
The fact is that you’re substracting your imageNumber, but you’re not reading your array with it. Add this at the end of your BatteryMinus and BatteryPlus func:
GetComponent().sprite= array[imageNumber];
I’m so stupid. It works now, thanks for your help!
1 Like