Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Lawlhwut · Jul 04, 2015 at 06:56 AM · raycastgridonmousedowngrid based game

Saving Grids in Grid System

I'm creating a grid system and allowing players to utilize multiple units to put furniture. The problem is, I have a script that is not working the way I intend:

  1. Units hovered over change material color to red

  2. Units that are clicked are saved in a list

  3. All units saved in the list are changed to blue

I have 1 and part of 2 finished. The problem is with 2, it seems like only one item is being saved. And when I run 3, the colors do not change at all.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class GridUnitScript : MonoBehaviour {
     public Vector3 originalPos;
     public Vector3 currentPos;
     public GameObject GridUnit;
     public Rect selectionBox;
 
     public List<Collider> unitsClicked = new List<Collider>();
     
     void selections() {
         
     }
 
     void Start () {
         //Change all color of units to Gray. Replace this with material later on.
         GetComponent<Renderer>().material.color = Color.gray;
     }
 
     void Update () {
         //Rectangle for box selection for future.
 /*            selectionBox = new Rect(Mathf.Min(originalPos.x, currentPos.x), 
                                     Mathf.Min(originalPos.y, currentPos.y),
                                     Mathf.Abs(originalPos.x - currentPos.x),
                                     Mathf.Abs(originalPos.y - currentPos.y));    */
     }
     /*void OnMouseEnter() {
         GetComponent<Renderer>().material.color = Color.red;
     }*/
     void OnMouseOver() {
         //Change color to blue when mouse is hovered over. This is working
         GetComponent<Renderer>().material.color = Color.blue;
     }
     void OnMouseExit() {
         //Change color back to gray when mouse moves away from unit. This is also working
         GetComponent<Renderer>().material.color = Color.gray;
     }
     void OnMouseDown() {
         //Create raycast when mouse is pressed down over collider
         RaycastHit hitInfo = new RaycastHit();
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if (Physics.Raycast(ray, out hitInfo)) {
             //Add the hit collider into the unitsClicked list
             unitsClicked.Add(hitInfo.collider);
             //Go through the list and change color to red
             foreach (Collider col in unitsClicked) {
                 col.GetComponent<Renderer>().material.color = Color.red;
                 Debug.Log(col);
             }
         }
 
     }
     void OnMouseDrag() {
 
     }
 }
 



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
0
Best Answer

Answer by Lawlhwut · Jul 04, 2015 at 11:05 AM

So I figured out the problem. The problem was the first two onmouse functions were doing stuff so I just had to put conditionals.

Something like this: using UnityEngine; using System.Collections; using System.Collections.Generic;

 public class GridUnitScript : MonoBehaviour {
     public Vector3 originalPos;
     public Vector3 currentPos;
     public GameObject GridUnit;
     public Rect selectionBox;
 
     public List<Collider> unitsClicked = new List<Collider>();
     
     void selections() {
         
     }
 
     void Start () {
         //Change all color of units to Gray. Replace this with material later on.
         GetComponent<Renderer>().material.color = Color.gray;
     }
 
     void Update () {
         //Rectangle for box selection for future.
 /*            selectionBox = new Rect(Mathf.Min(originalPos.x, currentPos.x), 
                                     Mathf.Min(originalPos.y, currentPos.y),
                                     Mathf.Abs(originalPos.x - currentPos.x),
                                     Mathf.Abs(originalPos.y - currentPos.y));    */
     }
     /*void OnMouseEnter() {
         GetComponent<Renderer>().material.color = Color.red;
     }*/
     void OnMouseOver() {
         //Change color to blue when mouse is hovered over. This is working
         if (GetComponent<Renderer>().material.color != Color.red) {
             GetComponent<Renderer>().material.color = Color.blue;
         }
     }
     void OnMouseExit() {
         //Change color back to gray when mouse moves away from unit. This is also working
         if (GetComponent<Renderer>().material.color == Color.blue) {
             GetComponent<Renderer>().material.color = Color.gray;
         }
     }
     void OnMouseDown() {
         //GetComponent<Renderer>().material.color = Color.red;
         //Create raycast when mouse is pressed down over collider
         RaycastHit hitInfo = new RaycastHit();
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if (Physics.Raycast(ray, out hitInfo)) {
             //Add the hit collider into the unitsClicked list
             unitsClicked.Add(hitInfo.collider);
             //Go through the list and change color to red
             foreach (Collider col in unitsClicked) {
                 col.GetComponent<Renderer>().material.color = Color.red;
                 Debug.Log(col);
             }
         }
 
     }
     void OnMouseDrag() {
 
     }
 }
 
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

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

Related Questions

Problem with raycast detection? 1 Answer

OnMouseDown to return object name 3 Answers

Detecting OnMouseDown when two objects are overlapping 0 Answers

Approach to Sensing in a 4x4 Grid 1 Answer

2x2 grid instead of 1x1 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