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 Ozale · Feb 10, 2011 at 12:06 PM · raycastselection

Toggling active between objects using a boolean

Hey guys,

I'm having a little difficulty with a unit selection system I'm working on. I have a script (see below) attached to two objects, and what I'd like is for the player to click on one, and it become 'activeUnit'. As a visual aid, I've made them change colours depending on if they are active or inactive.

My problem is this : I can select them fine, but for the life of me I can't devise a way to only select one at a time. When you click on object B, I need object A to be unselected, and visa versa.

If anybody could point me in the right direction I'd be very grateful.

private var activeUnit : boolean = false;

function OnMouseDown () { var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition); var select : RaycastHit;

 if (Physics.Raycast (ray, select, 100.0))
 {
     //print ("selected " +select.collider.gameObject.name);
     activeUnit = true;
 } else {activeUnit = false;}

}

function Update () {

 if (activeUnit==true) {
     renderer.material.color = Color.green;
 } else if (activeUnit==false){renderer.material.color = Color.red;}

}

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

2 Replies

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

Answer by tool55 · Feb 10, 2011 at 02:35 PM

var myCubes : GameObject[];

function Start() { for (var thisCube in myCubes) { thisCube.renderer.material.color = Color.red; } }

function Update () { if (Input.GetButtonDown ("Fire1")) { var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition); var select : RaycastHit;

 if (Physics.Raycast (ray, select, 100.0))
 {
 for (var thisCube in myCubes)
 {
 if( select.collider.gameObject ==thisCube)
 {
 thisCube.renderer.material.color = Color.green;
 }
 else
 {
 thisCube.renderer.material.color = Color.red;
 }

} } } }

Comment
Add comment · Show 10 · 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 Ozale · Feb 10, 2011 at 09:09 PM 0
Share

That almost works, the only issue is it affects every unit with this script attached. It also toggles them active/inactive no matter what I click.

avatar image tool55 · Feb 11, 2011 at 12:08 AM 0
Share

You would have to use your "select" variable to find which collider has been hit by the raycast and then check the tag or name or other parameter in an if statement to have it respond to that specific game object. Also, I wouldn't put this script on multiple game objects. Seems like it would be better to create variables for the game objects, then drag those game objects to the variable in the inspector.

avatar image tool55 · Feb 11, 2011 at 12:23 AM 0
Share

Actually, I think I misinterpreted what you want. I'll rework the script when I have bit of time later. I thought you wanted to toggle between two objects on each click, but you want to make them selectable. $$anonymous$$y bad.

avatar image tool55 · Feb 11, 2011 at 02:32 AM 0
Share

O$$anonymous$$. I modified the script above so that only one object is selected at a time. I made it an array so that you can have as many objects as you like. The one that's clicked will turn green. All others will turn red.

avatar image Ozale · Feb 11, 2011 at 05:38 AM 0
Share

What does this need (scene wise) to work? I've just got the same script applied to two spheres at the moment. I'm beginning to think I may need to learn how to build a class for 'units', so that I can target them externally, without needing to name each instance (and avoid limiting the amount of units). Sound right?

Show more comments
avatar image
1

Answer by Jessy · Feb 10, 2011 at 01:47 PM

First off, OnMouseDown already uses raycasting, so don't bother with that, unless you really need that distance of 100. I don't know what your script is called, or what else you have going on. I might do it this way, in JavaScript:

var otherUnit : Unit; @HideInInspector var _active : boolean = false;

function OnMouseDown () { _active = true; renderer.material.color = Color.green; otherUnit._active = false; otherUnit.renderer.material.color = Color.red; }

However, I really don't feel that JS is cut off for this kind of stuff. If you use C#, there is an event system, which can improve the code in many ways. If you have interest in this, and don't understand it, feel free to ask any questions about it you want.

using UnityEngine;

public class Unit : MonoBehaviour {

// "new" allows you to override the deprecated Component.active, // but is active even necessary? new bool active; new Renderer renderer;

delegate void EventHandler (); event EventHandler MouseDown; static event EventHandler SwitchActive;

void Awake () { renderer = GetComponent<Renderer>(); MouseDown += Activate; }

void OnMouseDown () {if (MouseDown != null) MouseDown();}

void Activate () { if (SwitchActive != null) SwitchActive(); active = true; renderer.material.color = Color.green; MouseDown -= Activate; SwitchActive += Deactivate; }

void Deactivate () { active = false; renderer.material.color = Color.red; SwitchActive -= Deactivate; MouseDown += Activate; }

}

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.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Help - Selection System 1 Answer

Math/Programming Question 4 Answers

area selection (mouse drag) on overhead game 1 Answer

Advance when the sound is done playing 1 Answer

Raycast - screen position to world position not precise 0 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