Jump to content
This site uses cookies! Learn More

This site uses cookies!

armedunity.com uses cookies to improve user experience.

By continuing to use this site, you agree to allow us to store cookies on your computer.

  • 0
Sign in to follow this  
ProExtremePL

For Loop problem

Question

Hey guys, I think my problem is very easy but I'm dumb.

I need to make pick up script but now when I try to pick up something only name0 is getting activaved.

With first item to pick up it's OK but when I'm picking up another, name0 is getting activated, no others.

If I delete break, then all are getting activated at once :/

 

Maybe I'm going in wrong direction, should think about another solution for this?

Please login or register to see this code.

 

Hope you guys help!

Edited by ProExtremePL

Share this post


Link to post
Share on other sites

12 answers to this question

Recommended Posts


  • 1

What this code is doing is checking the sticksMan.stick.Length, and for that length you are checking if the game object referenced is active in the hierarchy window (reference is pointing to the game object sticksMan.transform children).

If the first condition doesn't check out during the loop, it checks if the specified object is not active in the hierarchy window, then sets foundStick to active as a gameobject, destroys the object instance itself afterwards then sets the foundStick object under same reference as earlier.

 

By the way, break terminates the loop.

 

I think you may want to rethink your logic here. Personally your request is way too unclear for me to try and solve, maybe go in-depth about what you're trying to do?

 

 

Share this post


Link to post
Share on other sites
  • 0

Acctualy - sticksMan.sticks.Length - doesn't affect anything, can replace it with 4.

 

I meant that if object is active(first condition) then i++ and try to find next object.

I'm doing 3D inventory and that what's I'm trying to do is to add item(called as 'name' here) and if first one is set as actived, what is name0, then loop is trying to asign it to the next one what is name1, name2, name3 etc.

 

Loop has to break when item is added, am I right? And that's what I wanted to do.

 

Now I think that's little messy but with good intentions, maybe will look at some tutorial of doing 2D inventory to see how their system work or will get help :)

Edited by ProExtremePL

Share this post


Link to post
Share on other sites
  • 0

The issue might lie in the fact that your destroying the gameobject which terminates the script and found stick is never set and also breaking the loop on the first iteration whether it found it or not. only break if it does what you want it to, not every iteration

Share this post


Link to post
Share on other sites
  • 0

In the else if statement, remove Destroy(gameObject); and after foundStick = ... put break; or Destroy(gameObject); if your wanting to destroy it

 

edit: actually, completely remove foundStick = ... bc it's not even doing anything

Share this post


Link to post
Share on other sites
  • 0
Please login or register to see this quote.

 

Stick pick up item and stick in inventory are completely another objects so I think it doesn't affect on it.

But even that was true, script should check at first if name0 is activated

Share this post


Link to post
Share on other sites
  • 0

The point is your breaking during the iteration whether it found it or not, you can't do that bc the iteration never goes higher than 0. Only break AFTER it found the non active object and set it to true

Share this post


Link to post
Share on other sites
  • 0
Please login or register to see this quote.

 

But how will script know now that if name0 or another are activated and don't touch and set name1 as true?

Share this post


Link to post
Share on other sites
  • 0

And FYI, activeInHierarchy mean selected in the hierarchy. If you wanting to know if it's active or not in the scene you have to use if(someObject.activeSelf)

  • Like 1

Share this post


Link to post
Share on other sites
  • 0
Please login or register to see this quote.

 

Thank you some much dude!

Not it works great and even take less space :d

 

Love you <3 xD

 

And actually the biggest problem was activeInHierarchy, when I set it as activeSelf everything started working great, one more thanks!

Edited by ProExtremePL

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...