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 Jdfskitz · Nov 01, 2012 at 07:40 AM · gameobjectclickenablemeshrenderervisible

Making a Game object appear and dissapear

I am attempting for an RTS Style, and I want this box to disappear, but for some reason it wont do anything.

 var isSelected : boolean;
 var mesh_on : boolean;
 var currentUnit : GameObject;
 var speed = 240.0;
 
 isSelected = false;
 mesh_on = false;
 
 function Update () {
 
 var Units = 1 << 8;
 var Ground = 1 << 9;
 
 var ray = camera.ScreenPointToRay(Input.mousePosition);
 var hit_unit: RaycastHit;
     if(Input.GetMouseButton(0)){
     var tempUnit;
         if(Physics.Raycast(ray, hit_unit, Mathf.Infinity, Units)){
         print(hit_unit.transform.name);
             if(hit_unit.collider.gameObject.tag=="Units"){
                 currentUnit = hit_unit.collider.gameObject;
 

tempUnit = currentUnit.MeshRenderer; isSelected = true; if(isSelected){ tempUnit.GetComponent(MeshRenderer).enabled = true; }else{ tempUnit.GetComponent(MeshRenderer).enabled = false; } }

         }    
         else{
         print("you hit nothing");
         currentUnit = null;

 &#9;&#9;isSelected &#61; false;
  &#9;&#9;&#9;&#9;&#9;if(isSelected){
  &#9;&#9;&#9;&#9;&#9;&#9;tempUnit.GetComponent(MeshRenderer).enabled

= true; }else{ tempUnit.GetComponent(MeshRenderer).enabled = false; } }

     }
     
     var pos = (Time.deltaTime * speed);
     
     if(Input.GetKey("t")){
     currentUnit.transform.Translate(0, 0, pos);
     }
 
     if(Input.GetKey("g")){
     currentUnit.transform.Translate(0, 0, -pos);
     }
     
     if(Input.GetKey("h")){
     currentUnit.transform.Translate(pos, 0, 0);
     }
     
     if(Input.GetKey("f")){
     currentUnit.transform.Translate(-pos, 0, 0);
     }
     
 var ray_ground = camera.ScreenPointToRay(Input.mousePosition);
 var hit_ground: RaycastHit;
 }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by deltamish · Nov 01, 2012 at 11:46 AM

LoL Even you try to make thing complex

Use .active = false;

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 attar033 · Nov 01, 2012 at 10:21 PM 0
Share

I assumed he just wanted to make the object disappear. Using .active = false will also disable the collider and will not allow him to make the object reappear.

avatar image Jdfskitz · Nov 02, 2012 at 03:27 AM 0
Share

Correct. And it's still not working though

avatar image deltamish · Nov 02, 2012 at 05:40 AM 0
Share

$$anonymous$$y bad,I feel like an idiot

avatar image
0

Answer by CuddleMonster · Nov 01, 2012 at 11:47 AM

to Destroy the object Do This: Destroy(gameObject);

to turn it invisible but it still function normally... Do This: GetComponent(MeshRenderer).enabled = false;

to turn the object off, but not destroy it (you can turn it back on again later) Do This: gameObject.active = false;

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 Jdfskitz · Nov 02, 2012 at 03:28 AM 0
Share

If I do that then it tries to turn off the mesh renderer of the camera

avatar image CuddleMonster · Nov 02, 2012 at 03:33 AM 0
Share

obviously if the script is on the camera and you write just GetComponent($$anonymous$$eshRenderer).enabled = false; it will not work. you need to give it an object reference.

SelectedUnit.GetComponent($$anonymous$$eshRenderer).enabled = false;

I'd help you figure out how to work it exactly but since you don't comment your work its a little difficult.

avatar image
0

Answer by attar033 · Nov 01, 2012 at 11:51 AM

In your code, every unity has to do a Raycast check to see if it is selected. This is abit of an overkill. I would suggest that you have a different script that manages user input. This would look something like this (in c#)

 public class InputManager : Monobehaviour
 {
 public LayerMask unitsMask;
 
 void Update()
 {
 
     Ray ray = camera.ScreenPointToRay(Input.mousePosition);
     RayCastHit hit;
         
     if(Input.GetMouseButtonDown(0) && Physics.Raycast(ray, out hit, Mathf.Infinity,unitsMask ) )
     {
         UnitController unit = hit.transform.GetComponent<UnitController>();
             if(unit!=null) //this check is not really needed as the units will all have the same unique layer
             {
                //I just did a simple selection toggle. you might need a more complex user selection behaviour
                 unit. isSelected = !unit.isSelected;
                 unit.transform.gameObject.renderer.enabled = unit.isSelected
             }
     }
 
     }
 }

I apologise if I wrote this in C# but I think you get the general idea.

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

12 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

Related Questions

How to basically use GUI.Toggle? 2 Answers

Instantiated GameObject Not Rendering 1 Answer

enable and disable boxcollider (whats wrong with my script?) 2 Answers

How can I control GameObject visibility? 2 Answers

Game Objects enable problems 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