Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by derooie · Sep 30, 2015 at 04:52 AM · objectupdate function

Why doesnt my gameobject get filled?

I am trying to create a select object script in unity.

What it should do is when i hover over an object it colors red (and it does) and when i press "1" the GameObject targetSelected will be filled with the object i am hover on on that moment. In a Debug.Log this all works fine, the targetHighlighted is filled.

When i press "1" however the targetHighlighted object is empty. It doesn't matter if i press it still on the object or away from it.

Ofcourse the code was more extensive to use it the way i want. But in this code is the problem, so i reduced it to this.

Can anyone help me why when I press "1" the the Debug.Log doesn't show the targetHighlighted?

Basicly why do the mouseenter and mouseexit log the right Object, and the setTarget function doesnt?

Thanks in advance.

 using UnityEngine;
 using System.Collections;

 public class TargetSelectionScript: MonoBehaviour {
 GameObject targetHighlighted;
 GameObject targetSelected;
 Renderer rend;
 Color initialColor = Color.white;
 Color selectedColor = Color.red;
 public GameControllerScript gameController;

 void Start() {

 }

 void Update() {
     if (Input.GetKeyDown("1")) {
         SetTarget();
     }
 }

 void OnMouseEnter() {
     SelectTarget();
 }

 void OnMouseExit() {
     ClearTarget();
 }

 void SelectTarget() {
         RaycastHit hitInfo = new RaycastHit();
         Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
         targetHighlighted = hitInfo.transform.gameObject;
         rend = targetHighlighted.GetComponent < Renderer > ();
         rend.material.color = selectedColor;
         Debug.Log("Selected target: " + targetHighlighted);
     
 }

 void ClearTarget() {
     Debug.Log(targetHighlighted);
 }

 void SetTarget() {
     Debug.Log(targetHighlighted);
 }

}

Comment
Add comment · Show 2
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 Denvery · Sep 30, 2015 at 09:07 AM 0
Share

Hello! Sorry, I can't see when targetSelected is assigned. TargetHighlighed is assigned in line 34, and targetSelected only checked in line 31 and no assingment in all the code. $$anonymous$$ay be you have posted only part of your code?

avatar image derooie · Sep 30, 2015 at 10:16 AM 0
Share

Hi, youre right about targetSelected. But as you can see in the code, i dont use it. I had much more code, but brought it down to this. The question is why targetHighlighted is filled via mouseenter and mouseexit, but not when pressed the "1" key.

Since i dont do anything with targetSelected, only declare it, targetSelected = null.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by bubzy · Sep 30, 2015 at 10:41 AM

OnMouseEnter() is for the object that you are entering, not the main script. see here

i would find a better way of selecting targets, maybe put your selectTarget() routine in Update()

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 derooie · Sep 30, 2015 at 12:26 PM

@bubzy

Hi, thanks for youre reply. I was under the impression to use OnMouseEnter because it only is triggered once per object enter.

But if its better to use selecting the target via the Update function, I will give it a try tonight. Thanks.

PS: I still am very curious though, why my current code doesnt work ;)

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

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

Related Questions

Text object updating on everything else except for variable 0 Answers

Activating an object after it's set as inactive? 0 Answers

How do you pick up an instantiated object? 1 Answer

Start moving unchecks itself 0 Answers

how to make an object change size automatically when playing so it doesnt go through objects 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