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
-2
Question by CubePhysics · Feb 02, 2013 at 08:09 PM · c#bugspawnlock

Help with clearing bugs in this C# script

Hey I need help. I made a script and Im having allot of troubles. When I press "I" It unlocks the mouse and brings up the GUI spawn menu and when I press it again it it closes but if I click on a button and spawn something it locks the mouse again and When I press "I" it unlocks the mouse again but it doesnt hide the buttons again also I want the Mouse look script to be disabled when the spawn menu is showing and enabled when its being hidden. How do I fix this (including the mouse locking again when something is spawned) Here is the script:

 using UnityEngine;
 using System.Collections;
  
 public class SpawnGui : MonoBehaviour 
 {
 
     private bool showGUI;
     public bool lockCursor = true; 
     public Transform Cube;
     public Transform Cuboid;
     public Transform BlockHead;
     public bool CubeC;
     public bool CuboidC;
     public bool BlockHeadC;
     
     void OnMouseDown ()
     {
     
     }
     
     
     void Start () 
     {
        showGUI = false;
         
         Screen.lockCursor = true;
         
         if(Input.GetKeyDown(KeyCode.Escape))
         {
             Application.LoadLevel(0);
         }
         
         
     }
         
     
         
     
     void Update () 
     {
     
         if(CuboidC)
         {
             if (Input.GetMouseButtonDown(1))
             renderer.material.color = Color.red;
             
             if (Input.GetMouseButtonUp(1))
             {
             Destroy(gameObject);
             }
         }
         if(CubeC)
         {
             if (Input.GetMouseButtonDown(1))
             renderer.material.color = Color.red;
             
             if (Input.GetMouseButtonUp(1))
             {
             Destroy(gameObject);
             }
         }
         if (BlockHeadC)
         {
             if (Input.GetMouseButtonDown(1))
             renderer.material.color = Color.red;
             
             if (Input.GetMouseButtonUp(1))
             {
             Destroy(gameObject);
             }
         }
 
        if(Input.GetKeyDown(KeyCode.I))
        {
             
         
             
          
       
        
          if(showGUI == true)
          {
           showGUI = false;
           Screen.lockCursor = true;
          }
  
  
          else if(showGUI == false)
          {
           showGUI = true;
           Screen.lockCursor = false;
          }
             
             
           
        }
     }
  
 
     void OnGUI()
     {
       
        if(showGUI == true)
        {
          if(GUI.Button(new Rect(20,20,100,80), "Cube"))
          {
            Instantiate(Cube,GameObject.Find("spawnPoint").transform.position, Quaternion.identity);
          }
             
          if(GUI.Button(new Rect(20,120,100,80), "Cuboid"))
          {
            Instantiate(Cuboid,GameObject.Find("spawnPoint").transform.position, Quaternion.identity);
          }
             
          if(GUI.Button(new Rect(20,220,100,80), "BlockHead"))
          {
            Instantiate(BlockHead,GameObject.Find("spawnPoint").transform.position, Quaternion.identity);
          }
  
        }
     }
     
     
     
 }
Comment
Add comment · Show 6
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 CubePhysics · Feb 02, 2013 at 09:02 PM 0
Share

Why are there 2 dislikes? I just need help

avatar image Wolfram · Feb 02, 2013 at 09:08 PM 0
Share

I didn't vote you down, but the explanation in your question consists of a single uncomprehensible sentence that goes on and on over 5 lines. Plus, a script dump, while sometimes helpful/necessary, means everybody needs to invest considerable time to try and understand your code before they can try to answer.

avatar image iwaldrop · Feb 02, 2013 at 09:09 PM 1
Share

Let's slim things down some, shall we?

 public GameObject cube;
 public GameObject cuboid;
 public GameObject blockHead;
 public Transform spawnPoint;
 
 private GameObject
   cubeInstance,
   CuboidInstance,
   blockHeadInstance;
 
 void Awake() 
 {
   Screen.lockCursor = true;
 }
 
 void Update()
 {
   if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Escape))
    Application.LoadLevel(0);

   if (cubeInstance != null && Input.Get$$anonymous$$ouseButtonDown(1))
     StartCoroutine(DestroyObject(cubeInstance));
 
   if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.I))
     Screen.lockCursor = !Screen.lockCursor;
 }
 
 void OnGUI()
 {
   if (Screen.lockCursor) return;
 
   if (GUI.Button(new Rect(20,20,100,80), "Cube"))
     cubeInstance = Instantiate(cube, spawnPoint.position, Quaternion.identity) as GameObject;
 }
 
 IEnumerator DestroyObject(GameObject go)
 {
   const float TI$$anonymous$$E_TO_LIVE = 0.25f
 
   go.renderer.material.color = Color.red;
   yield return new WaitForSeconds(TI$$anonymous$$E_TO_LIVE)

   if (go != null)
     Destroy(go);
 }

I didn't put in all the object types that you have; it'll be quite simple to get them in there. This might have even solved your problem; and now you can actually load the next level by pressing the escape key. ;)

avatar image Wolfram · Feb 02, 2013 at 09:10 PM 0
Share

Plus, it might be beneficial to use phrases such as "Hi there! Could somebody help me out please?", ins$$anonymous$$d of "Hey I need help."...

avatar image T27M · Feb 02, 2013 at 09:11 PM 1
Share

This ^ but anyone down voting without reason doesn't help him either.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by T27M · Feb 02, 2013 at 09:09 PM

Could you explain your first problem a little more I'm not seeing the problem you say you encounter when you click the GUI button.

Assuming that the MouseLook script is attached to the same gameobject as this script you can use

         if(Input.GetKeyDown(KeyCode.I))
         {
             if(showGUI == true)
             {
                 showGUI = false;
                  Screen.lockCursor = true;
                 gameObject.GetComponent<MouseLook>().enabled = true;
             }    
             else if(showGUI == false)
             {
                 showGUI = true;
                  Screen.lockCursor = false;
                 gameObject.GetComponent<MouseLook>().enabled = false;
             }
         }

If not you will have to use a reference to the MouseLook component.

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Random element from the list 1 Answer

Character refuses to turn 0 Answers

Make a random number get chosen less or more? (C#) 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