- Home /
Why letters appear in reversed order ? ,Letters appear in reversed order
Hello,
I'm building a game where player have to draw a path between some letters to make a word. The problem is that the letter appears in reversed order of the path.
Things I tried: * changing for-loop to :
for(int i = (offeredLetters.Count- 1); i >= 0; i--) // Nothing change
*tried foreach
I appreciate any help.
The image below show the problem.
Here is my code:
public void CreateOfferedLetters()
{
float alpha = 360f / (offeredLetters.Count + offeredBonusLetters.Count);
float firstAngle = 0;
Vector3 startPosition = new Vector3(0, 250f, 0);
for (int i = 0; i < offeredLetters.Count; i++)
{
GameObject letter = Instantiate(offeredLetterPrefab, offeredLettersHolder.transform) as GameObject;
letter.transform.Find("AnimationHolder/LetterHolder").GetComponent<OfferedLetter>().letter = offeredLetters[i];
letter.transform.Find("AnimationHolder/LetterHolder/LetterImage").GetComponent<Image>().sprite = GetLetterSprite(offeredLetters[i]);
letter.transform.localScale = Vector3.one;
letter.transform.localPosition = Vector3.zero;
Quaternion r = letter.transform.rotation;
r.eulerAngles = new Vector3(0, 0, firstAngle);
letter.transform.rotation = r;
Quaternion l = letter.transform.Find("AnimationHolder/LetterHolder").localRotation;
l.eulerAngles = new Vector3(0, 0, -firstAngle);
letter.transform.Find("AnimationHolder/LetterHolder").localRotation = l;
firstAngle += alpha;
// Create a selection letter
GameObject sl = Instantiate(selectedLetter, selectedLettersHolder.transform) as GameObject;
sl.GetComponent<Image>().sprite = GetLetterSprite(offeredLetters[i]);
sl.transform.localScale = Vector3.one;
sl.transform.localPosition = Vector3.zero;
sl.name = offeredLetters[i];
sl.SetActive(false);
}
Answer by cryingwolf85 · Jul 31, 2018 at 02:53 PM
Technically, you aren't printing the numbers in 'reverse', you are printing them in ascending order. What you want IS to print them in reverse.
What you had attempted before is correct, but you have a syntax error, you have a random '.' after .Count. Replace your for loop with the following and it should work like you want:
for (int i = offeredLetters.Count - 1; i >= 0; i--)
I doubt it would compile if the '.' was left alone. It was probably a bad copy/paste.
Possibly a bad copy/paste, regardless reversing the order should work.
Uh, i don't think it implied it was working fine.
Could you try this (again) and verify whether or not it works? If not we'll need to see more code, I'm not seeing anything showing the specific Vector each letter is spawned at.
Answer by madks13 · Jul 31, 2018 at 02:55 PM
The fact that nothing changed when you reversed the order is not normal. What type is offeredLetters?
@AbdoQum try reversing the list with Linq :
var reversed = offeredLetters.Reverse();
then using reversed when retrieving letters.
Edit : @AbdoQum : somehow comments can't handle special characters. I'm tyring to write the right way to call Reverse :
var rev = offeredLetters.Reverse<string>();
I just realized, but which part actually creates the letter that are in reversed order? I'm guessing that if you use angles, the variable 'letter' is for the lower part, where letters are displayed in circular order. But i don't see you getting the ordered letters. You just use the same offeredLetters variable. Where do you store the order in which the letters were selected?
@madks13 I tried .Reverse but compile throw an error
An implicitly typed local variable declaration cannot be initialized with `void'
here is my code with reversing with linq
public void CreateOfferedLetters()
{
float alpha = 360f / (offeredLetters.Count + offeredBonusLetters.Count);
float firstAngle = 0;
Vector3 startPosition = new Vector3(0, 250f, 0);
var rev = offeredLetters.Reverse ();
foreach (string value in rev)
{
GameObject letter = Instantiate(offeredLetterPrefab, offeredLettersHolder.transform) as GameObject;
letter.transform.Find("AnimationHolder/LetterHolder").GetComponent<OfferedLetter>().letter = offeredLetters[value];
letter.transform.Find("AnimationHolder/LetterHolder/LetterImage").GetComponent<Image>().sprite = GetLetterSprite(offeredLetters[value]);
letter.transform.localScale = Vector3.one;
letter.transform.localPosition = Vector3.zero;
Quaternion r = letter.transform.rotation;
r.eulerAngles = new Vector3(0, 0, firstAngle);
letter.transform.rotation = r;
Quaternion l = letter.transform.Find("AnimationHolder/LetterHolder").localRotation;
l.eulerAngles = new Vector3(0, 0, -firstAngle);
letter.transform.Find("AnimationHolder/LetterHolder").localRotation = l;
firstAngle += alpha;
// creating a letter for selection
GameObject sl = Instantiate(selectedLetter, selectedLettersHolder.transform) as GameObject;
sl.GetComponent<Image>().sprite = GetLetterSprite(offeredLetters[i]);
sl.transform.localScale = Vector3.one;
sl.transform.localPosition = Vector3.zero;
sl.name = offeredLetters[i];
sl.SetActive(false);
}