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
0
Question by ardizzle · Jul 09, 2013 at 02:09 AM · gameobject

How to choose between two diffrent idectical objects by clicking?

Im trying to write a script so that when you click on a square you can choose it and change its color. Right now I have two squares that are identical in everything but their X and Y position. I also have a red and a blue rectangle that change the color of the sqaures when you click on them. I need to know how to change the color of the selected square instead of both at the same time.

 #pragma strict
 private var nonSelectedObject : GameObject;
 private var selectedObject : GameObject;
 function Start () 
 {
 
 }
 
 function Update () 
 {
     EditMode();
 }
 
 function EditMode()
 {
     if(Input.GetMouseButtonDown(0))
     {
         var hit : RaycastHit;
         var ray : Ray = camera.main.ScreenPointToRay(Input.mousePosition);     
         if(Physics.Raycast(ray,hit,1000))
         {
             if(hit.transform // somthing goes here that i can't find ==  and here  )
             {
                 selectedObject = // also not sure what i need to set this equal to.
             }
             
             if(hit.transform != GameObject)
             {
                 selectedObject = null;
             }
             if(hit.transform.name == "Blue")
             {
                 selectedObject.renderer.material.color = Color.blue;
             }
             
             if(hit.transform.name == "Red")
             {
                 selectedObject.renderer.material.color = Color.red;
             }
         }
         
     }
 }
Comment
Add comment · Show 1
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 robertbu · Jul 09, 2013 at 02:16 AM 0
Share

Posting your script will help us see what is going on, and how to suggest you fix/change your code.

3 Replies

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

Answer by ardizzle · Jul 11, 2013 at 01:54 AM

Ok guys thanks a ton for your help. experimenting with your sugenstions led me to what i was looking for. Incase your curious how i solved it or if your looking for the awnswer your self this is the code I wrote :

 #pragma strict
 static var objectNumber         : int; // changes for every object created. Used for makeing each object unique
 private var thisObjectsNumber   : int;    // makes the static variable a non changing private varaible
 private var objectName             : String;        // the name for all objects.
 private var selectedObject         : GameObject; // var for your selected object. This is the object that gets changed.
 private var thisObject             : GameObject;    // sets the game object to this script
 
 function Start () 
 {
     // Gives every object its uniqe name when its created.
     objectNumber += 1;
     thisObjectsNumber = objectNumber;
     objectName = "Obj# " + objectNumber; 
     thisObject = gameObject;
     thisObject.name = objectName;
 }
 
 function Update () 
 {
     EditMode();
 }
 
 
 function EditMode()
 {
     
     if(Input.GetMouseButtonDown(0))
     {
         var hit : RaycastHit;
         var ray : Ray = camera.main.ScreenPointToRay(Input.mousePosition); 
         if(Physics.Raycast(ray,hit,100))
         {
             // makes sure to not nullify your object when you click off of it
             if(hit.transform.tag == "Game Object")
             {
                 if(hit.transform.name == objectName)
                 {
                     selectedObject = GameObject.Find(objectName);
                 }
                 
                 if(hit.transform.name != objectName)
                 {
                     selectedObject = null;
                 }
             }
             // keeps you from getting null refrence errors when changing colors with multiple game objects up
             if(selectedObject != null)
             {
                 if(hit.transform.name == "Blue")
                 {
                     selectedObject.renderer.material.color = Color.blue;
                 }
                 
                 if(hit.transform.name == "Red")
                 {
                     selectedObject.renderer.material.color = Color.red;
                 }
             }
         }
         
     }
 }

Just wanted to say thanks again guys;

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
avatar image
0

Answer by youngapprentice · Jul 09, 2013 at 02:29 AM

You most likely have them sharing the same material!

Also, you don't need to write a universal script for this! You can add a simple script to each square using OnMouseDown()

When the square senses onMouseDown, change a variable that holds a Transform reference to the squares to that square, and then use OnMouseDown() for the colored rectangles. Run a function that gets the renderer of the currently selected square and set the color :)

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 ardizzle · Jul 09, 2013 at 02:33 AM 0
Share

The problem isn't the material. I need the 2 cubes to be identical in every way. But I need it so i can click one while the game is playing and change is color and then i can click the other one and change its color with out disturbing the first cube.

avatar image ardizzle · Jul 09, 2013 at 02:35 AM 0
Share

The only problem I have with that is that as the game goes on i don't just plan on having you change color but also being able to move the object, change the size, and even make clones that can be modified separately.

avatar image
0

Answer by johnnymoha · Jul 09, 2013 at 03:06 AM

 IN Camera:
 var hit : RaycastHit;
 var ray: Ray;

 IN Camera UPDATE:
 if (Input.GetMouseButtonDown (0)) {    //If Click
     clickPos = Input.mousePosition;
     ray = camera.ScreenPointToRay (clickPos);
     if (Physics.Raycast (ray,hit)) {    //IF Click On Object
     hit.transform.gameObject.SendMessage("Click");
     }     
   }
 
 On Clickable object:
 function Click(){
      renderer.material.color = Color.Red;
 }
 
 should turn whatever you click on red.  
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 ardizzle · Jul 10, 2013 at 03:15 AM 0
Share

Thats close to what im looking for but I need to actually be able to select the game object its self so I can edit it in other ways like changing its size and position.

avatar image johnnymoha · Jul 10, 2013 at 07:28 AM 0
Share

I see. You do have a handle to the object's transform: "hit.transform" you can move just that object and scale just that object once you have the handle to it. That's what the ray trace returns for you in the hit.

avatar image Aeron0 · Jul 10, 2013 at 08:09 AM 0
Share

If your using 3d models/prefabs you want to use what johnnymoha suggested. That is RaycastHit hit and Ray ray. This checks if mouse cursor "touched a model or prefab". the prefabs/models will need a collider component for this to work though.

Then with the "hit" variable you can check which prefab was touched by mouse cursor in a few ways. one way is check the "hit" variable position with prefab positions in the scene to find which prefab was touched by the mouse. From there you know what prefab it was and so make changes to that prefab as you like.

 GameObject selectedPrefab;
 
 if (hit.transform.position == myCube.transform.position)
 {
   //code here to change Prefab myCube
   myCube.transform.something here

   //or make a reference to the found Cube
   selectedPrefab = myCube;
 
 }

If your not using 3dmodels or prefabs then you would want to use the GUI.

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

18 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 avatar image avatar image avatar image

Related Questions

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

Using script's method from all of the gameobjects that has that script 2 Answers

How to create 3D game object in specified pixel size? 2 Answers

Unity3D Pressure Plate request. 3 Answers

Trying to delay movement of a GameObject 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