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 Novrick · Oct 25, 2011 at 08:26 AM ·

Problem with script

I have made a game that you can spawn certain objects or destroy them, based on which bool is true/false.

it have worked great so far with the two first bools, but the third function (that makes that you can both destroy and create objects) isn't working for some odd reason.

here's the script:

 using UnityEngine;
 using System.Collections;
 
 public class spawnscript : MonoBehaviour {
     
 
 public GameObject cubeprefab;
 public GameObject rectangleprefab;
 public GameObject stairprefab;
 public GameObject spawncube;
 
 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
     
     
             
     if(Input.GetKeyDown("mouse 0"))
     {
         
         if(idea.Instance.shadow1 == true)
         {
         
     //Vector3 position = new Vector3(camera.transform.position.x + 2, camera.transform.position.y);
     
     Instantiate(cubeprefab, transform.position, transform.rotation);
     //Physics.IgnoreCollision(cubeprefab.collider, spawncube.collider);
         }
         
         if(idea.Instance.shadow2 == true)
         {
         //Instantiate(rectangleprefab, transform.position, transform.rotation);    
         
         Vector3 fwd = transform.TransformDirection(Vector3.forward);
         RaycastHit hit;
         if(Physics.Raycast(transform.position, fwd, out hit,10))
             {
                 Destroy(hit.collider.gameObject);
                 print("are ou hitting on me?");
             
         }
         
         if(idea.Instance.shadow3 == true)
         {
             
         Instantiate(cubeprefab, transform.position, transform.rotation);    
             
         }
         
         
     }
     
     
     if(Input.GetKeyDown("mouse 1"))
     {
         
     if(idea.Instance.shadow3 == true)
         {
             
         Vector3 fwd = transform.TransformDirection(Vector3.forward);
         RaycastHit hit;
         if(Physics.Raycast(transform.position, fwd, out hit,10))
         {
         
             Destroy(hit.collider.gameObject);
             print("are you hitting on me?...again?");        
                     
         }
             
         }
         
     }
     
     
     //if(Input.GetKeyDown("mouse 0"))
     //{
     //Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     //Vector3 fwd = transform.TransformDirection(Vector3.forward);
     //RaycastHit hit;
     //if(Physics.Raycast(transform.position,fwd, out hit,10))
         //{
         
         //Destroy(hit.collider.gameObject);    
         //print("are you hitting on me?");
             
             
         //}
         
     }
     
     
     
     
         
     }
     
     
     
     



}

Comment
Add comment · Show 1
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 syclamoth · Oct 25, 2011 at 08:43 AM 0
Share

Please, ask a specific question, don't just post your entire script and complain 'it's not working!'. It's hard to just look at something and see problems with it! Can you narrow it down to just one function, or line, which you know is causing the problem?

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by CHPedersen · Oct 25, 2011 at 08:38 AM

I took the liberty to clean up your code, removing things that were commented out as well as numerous redundant spaces between different lines. I've also indented the code properly so it becomes clearer what the scope of each line of code is, that is, which if-sentences they belong to.

Cleaned up, it looks like this:

public class spawnscript : MonoBehaviour { public GameObject cubeprefab; public GameObject rectangleprefab; public GameObject stairprefab; public GameObject spawncube;

 // Update is called once per frame
 void Update()
 {
     if (Input.GetKeyDown("mouse 0"))
     {            
         if (idea.Instance.shadow1 == true)
             Instantiate(cubeprefab, transform.position, transform.rotation);

         if (idea.Instance.shadow2 == true)
         {
             Vector3 fwd = transform.TransformDirection(Vector3.forward);
             RaycastHit hit;
             if (Physics.Raycast(transform.position, fwd, out hit, 10))
             {
                 Destroy(hit.collider.gameObject);
                 print("are ou hitting on me?");
             }

             if (idea.Instance.shadow3 == true)
                 Instantiate(cubeprefab, transform.position, transform.rotation);
         }

         if (Input.GetKeyDown("mouse 1"))
         {
             if (idea.Instance.shadow3 == true)
             {
                 Vector3 fwd = transform.TransformDirection(Vector3.forward);
                 RaycastHit hit;
                 if (Physics.Raycast(transform.position, fwd, out hit, 10))
                 {
                     Destroy(hit.collider.gameObject);
                     print("are you hitting on me?...again?");
                 }
             }
         }
     }
 }

}

It now becomes clear that the code that is supposed to execute under the 3rd boolean is in fact embedded in the scope that belongs to the second boolean. Also, the code that checks to see if the user has pressed the right mouse button is embedded in the code that executes when the LEFT mouse button is pressed. So it'll likely never execute, because you probably can't get them both to be true in the exact same frame.

This is all confusion caused by improperly indenting code. Make sure you indent your code properly, so you know when one if-sentence begins and another ends.

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 Novrick · Oct 25, 2011 at 09:36 AM 0
Share

ah I see...thanks for the help =)

avatar image CHPedersen · Oct 25, 2011 at 09:43 AM 0
Share

No problem. :)

avatar image syclamoth · Oct 25, 2011 at 10:24 AM 0
Share

Good job taking that one on! Also, a good explanation of why proper formatting is important.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Zombie Round Script Help 1 Answer

Spawn Script Not Working 1 Answer

[SOLVED] Problem with "foreach". 1 Answer

Making A Script for my Switch 1 Answer

Bullet not moving 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