• 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
0
Question by membrainred3 · Jun 19, 2014 at 08:30 AM · gameobjects

How do I make game objects appear with OnMouseDown?

I have been trying to figure this out for weeks now and still having some trouble with this. I want to be able to click my text and have roughly 7 game objects appear in a selected position.

using UnityEngine; using System.Collections;

public class FormationToPlays : MonoBehaviour {

 public bool Play1 = false;
 public GameObject Player1 = null;
 public GameObject Player2 = null;
 public GameObject Player3 = null;
 public GameObject Player4 = null;
 public GameObject Player5 = null;
 public GameObject Player6 = null;
 public GameObject Player7 = null;
 
 void  OnMouseEnter ()
 {
     //change text color
     renderer.material.color=Color.black;
 }
 
 void  OnMouseExit ()
 {
     //change text color
     renderer.material.color=Color.white;
 }

 
 void OnMouseDown ()
 {
     //is this Play1
     if (Play1 ==true)
     {
         if (Player1 != null && Player1.gameObject != null)
             Player1.gameObject.enabled = !Player1.gameObject.enabled;
         if (Player2 != null && Player2.gameObject != null)
             Player2.gameObject.enabled = !Player2.gameObject.enabled;
         if (Player3 != null && Player3.gameObject != null)
             Player3.gameObject.enabled = !Player3.gameObject.enabled;
         if (Player4 != null && Player4.gameObject != null)
             Player4.gameObject.enabled = !Player4.gameObject.enabled;
         if (Player5 != null && Player5.gameObject != null)
             Player5.gameObject.enabled = !Player5.gameObject.enabled;
         if (Player6 != null && Player6.gameObject != null)
             Player6.gameObject.enabled = !Player6.gameObject.enabled;
         if (Player7 != null && Player7.gameObject != null)
             Player7.gameObject.enabled = !Player7.gameObject.enabled;
     }
 }

}

Comment
Add comment · Show 3
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 membrainred3 · Jun 19, 2014 at 08:26 AM 0
Share

anyway you could link me to somewhere that is c# based?

avatar image Kiwasi · Jun 19, 2014 at 08:33 AM 0
Share

First hint, use an array. DRY code is very important. What exactly is not working?

avatar image membrainred3 · Jun 19, 2014 at 08:37 AM 0
Share

I'll try using an array. And exactly well I am having problems with getting the objects to appear on click. ins$$anonymous$$d they are loading when I push play.

2 Replies

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

Answer by PommPoirAbricot · Jun 19, 2014 at 08:53 AM

Try that :

public bool Play1 = false; public GameObject[] Players = null;

 void OnMouseEnter()
 {
     //change text color
     renderer.material.color = Color.black;
 }

 void OnMouseExit()
 {
     //change text color
     renderer.material.color = Color.white;
 }


 void OnMouseDown()
 {
     //is this Play1
     if (Play1 == true)
     {
         foreach (GameObject p in Players)
         {
             if (p != null)
             {
                 p.SetActive(p.activeSelf);
             }
         }
     }
 }

SetActive() : to activate/ desactivate a gameobject

enabled : to activate/ desactivate a component

activeSelf (ReadOnly) : to checked if a gameobject is activated or desactivated

Comment
Add comment · Show 2 · 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 membrainred3 · Jun 19, 2014 at 09:01 AM 0
Share

thank you so much this really has helped me out a lot.

avatar image PommPoirAbricot · Jun 19, 2014 at 09:11 AM 0
Share

I would recommend my update

avatar image
0

Answer by OrbitGames · Jun 19, 2014 at 08:46 AM

Use Instantiate

a code example:

 void OnMouseDown()
 {
 Instantiate(prefab,Vector3.zero,Quaternion.identity);
 }
Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Find gameobjects by tag, and destroy if in the distance of a certain game object... 1 Answer

Grouping GameObjects by script 1 Answer

OnTriggerEnter() not firing for mutliple objects 0 Answers

Multiple boards 0 Answers

How would you convert this JavaScript code into C#? 1 Answer


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