Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by primus88 · Jun 04, 2013 at 05:14 PM · instantiateif-statements

How to check an entire list and apply conditions.

Hello and apologizes regarding the title

I have no idea how to put this better. My question is how to manage the following better:

I have 15 game objects instantiated randomly. Each object when clicked, will direct you to a scene (15 scenes in total)

How to attach each load scene to each game object efficient when clicked ?

My code idea is this :

 void SettingUpLocations()
     {
         if(Input.GetMouseButtonDown(0))
         {
             
         //SETUP EACH LOCATION
         if (clickedGmObject.name == "BattleLocation1(Clone)")
         {
             Application.LoadLevel ("FirstLevel");
         }
         }
     }  

I will iterate this 14 more times. I am pretty sure there is a more effective way of doing this. Any ideas ?

BTW I also have a list of the spawned prefabs made like this :

 prefabTransforms.Add(Instantiate(spawnThis[i].transform, spawnPoints[r].transform.position , spawnPoints[r].transform.rotation) as Transform);
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by cregox · Jun 04, 2013 at 06:44 PM

You can't use Input.GetMouseButton and expect it will know which object you clicked. It gathers mouse event regardless where it is clicking.

Make a class like this and name it `SettingUpLocations.js` (or whatever you wish):

 #pragma strict
 public var levelName = "";
 
 function OnMouseUp () {
     Application.LoadLevel(levelName);
 }

Add it to each of your game objects and set Level Name as you wish in the Inspector. Be sure the objects have colliders on them.

P.S.: You could name the question something like "How to check for mouse clicks on many different objects". Also, next time, try researching and looking at code on wiki first. Lots to learn there! ;-)

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image primus88 · Jun 04, 2013 at 06:54 PM 0
Share

You indirectly gave me ideas though my problem wasn't that the code wasn't working. I was using ray casting so I was identifying the objects correctly.

Putting this small script on each object is better than verifying 15 'if's in one method.

Thanks a lot!

PS: Also changed the title to something more correct.

avatar image cregox · Jun 04, 2013 at 07:03 PM 0
Share

@primus88 Well, you didn't mention ray casting. On$$anonymous$$ouseUp does have its caveats. It won't work for touching interfaces, for one. But there is a script to fix that, which does use Input and Raycasting.

Also, if you really want it to behave as a button, you may want to use `On$$anonymous$$ouseUpAsButton` ins$$anonymous$$d.

I also think putting the script on each object is better - as stated on the answer! No idea what indirect ideas I gave you there.

And I haven't noticed the title change.

avatar image primus88 · Jun 04, 2013 at 07:23 PM 0
Share

I was the referring to the idea with using a small script for each object. It is good.

I changed the title.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

conditional spawning 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

question about if statement order of execution 2 Answers

Split a cube into several pieces? 1 Answer

scripting question 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges