- Home /
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?");
//}
}
}
}
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?
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.
Good job taking that one on! Also, a good explanation of why proper formatting is important.
Your answer
Follow this Question
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