Arrays[Solved]

Updated on March 24, 2016 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
7 on March 24, 2016

im having a little trouble with my array, im not going to lie i havent used them allot which is why im struggling a little so any help or correctons to my code will be loved.

 public GameObject[] moonRock;
 void Start ()
 {
 for(int i = 0; i <= moonRock.Length; i++)
 {
 Debug.Log(moonRock[i].name);
 int s = 1;
 if(PlayerPrefs.GetInt("MoonRock " + s.ToString()) == 1)
 {
 moonRock[i].SetActive(false);
 }
 else
 {
 moonRock[i].SetActive(true);
 }
s++;
 }
 }
 }
 

basicly this script is to turn an image gameobject of with text in it which is why im using gameobject not image.
my player prefs start at 1 like “MoonRock 1”
and the array starts at 0 up to 13 at the moment
the array works but only if the first moonrock is there and when the moonrock is there it turns all gameobjects of in the array

  • Liked by
Reply
0 on March 24, 2016

 

hmm what i boob i am i belive iv solved my problem my self

public GameObject[] moonRock;
void Start ()
{
for(int i = 0; i <= moonRock.Length; i++)
{
Debug.Log(moonRock[i].name);
int s = (i+1);
if(PlayerPrefs.GetInt("MoonRock " + s.ToString()) == 1)
 { 
moonRock[i].SetActive(false);
 }
 else
 { 
moonRock[i].SetActive(true); 
}
} 
}
}

i belive it was s was never increasing so i changed it to int s = (i+1); if ive missed anything els can you reply to this and let me know thanks pete 🙂
  • Liked by
Reply
Cancel
3 on March 24, 2016

Looks good man! Glad you solved it yourself 😉
It’s not bad, I’ve done it too on this forum.
 
But yeah, you know?
You probably learned more this way than if anyone would’ve helped you.
So it’s all for the best 😀

Helpful
on March 24, 2016

indeed, i just miss place 1 peace code. At least now i know a little more about arrays and how they work.
im now currently trying to sort a timer issue, Only a minor bug but its a bug never the less.
If you have and knowledge on this topic, is there any chance i could pick your brain?

Guru
on March 24, 2016

I don’t really know what you mean…
Please use proper punctuation and capitalization next time.
 
I don’t know if you know about it, but you can step through your code line by line while it is running in Unity.
Here’s a useful link: https://unity3d.com/learn/tutorials/modules/beginner/scripting/monodevelops-debugger

Helpful
on March 24, 2016

I do apologise, I’m not so great with spelling and punctuation.
the link you provided doesn’t help, i have done a lot of research and im trying to find the best way for this certain timer i will try to explain.

Basicly when a button is pressed, display text when timer runs out stop, this bit is easy.
When the button is pressed again the timer doesn’t start from the top again.
So the text is only displayed for milliseconds.
This is the minor bug i have ran into.
Im also using IEnumerator, which by what i have been reading is not going to be the best way to achieve the effect i am after.

Show more replies
  • Liked by
Reply
Cancel
1 on March 24, 2016

So when you press the button, then the timer needs to be reset.
What’s the problem with that?
 
Just reset all the relevant variables to the default value.

Helpful
on March 24, 2016
Click to Expand

This is my current script it is very basic. you cannot reset IEnumerator that is my current problem i have tried to implement,
http://docs.unity3d.com/ScriptReference/MonoBehaviour.StopCoroutine.html
but on the second click it just staid active so i did even more research, apparently this is not the best way to go about this.

Show more replies
  • Liked by
Reply
Cancel