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 fschaar · Jul 06, 2012 at 12:05 PM · javascriptgameobjectvariable

Switching Gameobjects in a variable

I have the following task: I have some cubes, that the player can pick up and move to a slot (a simple plane). The moment the player moves over the slot, the planes should change its color to indicate that it can now be snapped into the slot. That works so far. But if the user decides to move the cube away from the slot, I have to assign the original color to that slot. For that I save the slot in a variable "currentSlot". I reassign the old color (doesn't work) and then I need to erase the currentSlot variable. But I do not know how to do that. As a workaround I assigned it a empty dummy object. But that's not a solution I'm satisfied with. Has anyone a better idea?

Here is my code so far:

 function GetSlot() //: GameObject 
     {
        var hit : RaycastHit; //Saves the hitted object 
        var ray = new Ray(transform.position, (-transform.up)); //check object under the cube
        if (Physics.Raycast (ray, hit, 10.0)){
        //Debug.Log(hit.collider.gameObject.tag);
        if(hit.collider.gameObject.tag =="slot")
          {
            currentSlot = hit.collider.gameObject;
            originalSlotColor = currentSlot.renderer.material.color;
            currentSlot.renderer.material.color = activeSlotColor;
          }
        else
          {
           currentSlot.renderer.material.color = originalSlotColor;
           currentSlot = GameObject.Find("DummyObject");
          
          }
        }
        Debug.Log(currentSlot);
     }
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

Answer by fschaar · Jul 09, 2012 at 11:52 AM

Hi! Thanks for your answer! the problem was, that the GetSlot function was inside the update function - so it reassigned the color permanently to the original color variable. So the second time it run through, it assigned the new color to the original color variable. After I fixed that by setting the original color in a permanent variable, i recogniced, that, if i move the cube very fast over the slots, sometimes the color stays or more slots change the color at the same time. That's not the goal. So I moved the code to the slot object. now each slot object checks if there is a cube above them and changes color if that is the cause. That works fine. But maybe I get a performance problem later...

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I change the text of a GUIText object through another GameObject using a variable? 2 Answers

Scripts accessing one another (JS) 0 Answers

How to deactivate something when holding down a key? 1 Answer

GameObject variable change for all of the objects 1 Answer

Set script variable to 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