- Home /
Boolean not switching?
So I have this scipt:
#pragma strict
private var inArea : boolean = false;
var missile : Transform;
var player : GameObject;
//BlastDistance being called from somewher else
function BlastDistance () {
inArea = true;
}
function OnTriggerEnter (meow : Collider) {
if (inArea == true) {
if (meow.gameObject.name == "Player") {
Debug.Log ("MEOW");
}
}
}
but the boolean never switches. If you want the script that called it :
#pragma strict
var partical2 : Transform;
var blastTrigger : Transform;
function OnCollisionEnter (col : Collision) {
if (col.gameObject.name == "Player") {
BroadcastMessage ("BlastDistance");
WaitForSeconds (0.1);
Destroy (col.gameObject);
}
Instantiate (partical2, this.transform.position, this.transform.rotation);
Destroy (this.gameObject);
}
Any help?
Thanks in advance.
For Broadcast$$anonymous$$essage to work the script that it broadcasts to must be on the same object or one of it's children, have you got it setup like this?
@$$anonymous$$rSoad It's being called by the parent and normally it says that there's no receiver if it doesn't work
Put some Debug.Log s in your OnCollisionEnter() function, at various points to find out what is and is not getting run at the moment.
@$$anonymous$$rSoad @Jeff $$anonymous$$esselman I did this script :
#pragma strict
private var inArea : boolean = false;
var missile : Transform;
var player : GameObject;
function BlastDistance () {
inArea = true;
Debug.Log ("in");
}
function OnTriggerEnter (meow : Collider) {
if (inArea == true) {
Debug.Log ("is true");
if (meow.gameObject.tag == "Player") {
Debug.Log ("Works");
}
}
}
and the only one that turned on was "in" Why isn't it turning true?
I need the objects complete, to see exactly how you have them setup and exactly how their various scripts behave and interact. I don't want to spend time trying to fix something that may be broken of affected be something else. $$anonymous$$eep trying I will have a look when you have given me it whole, thanks.
Answer by MrSoad · Nov 30, 2014 at 12:53 AM
Hi again, really sorry for not getting back to you sooner! Below are your scripts which are fixed, just replace these in your project. The main issue was that the Player object was being destroyed before the Blast Trigger code was executed, so the Player object was no longer in the blast area. I have moved all the destroy code and particle instantiate into the "stuff" script, now called "Blast_Script". The message triggers and everything works. I have put a quick comment in your "Missile" script about the use of yields, and have reformatted your "controller" code. You might want to rename the "controller" script to something more specific like "Player_Controller_Script", up to you though. Again sorry for the delay, Geometry Wars 3 Dimensions took over my life for a couple of days... :Dlink text
Don't worry about that :) You accepted the answer which is a lot more than some people do. Have a +1 for doing what many don't bother to do :D
Out of interest, is Geometry Wars 3 Dimensions a game you're working on?
Answer by tw1st3d · Nov 26, 2014 at 05:52 PM
Alright so I haven't worked with the Unity API in quite a while now, but I feel like you could solve this a bit easier with C#. However, I do see that you are working with UnityScript in the question, so feel free to figure it out otherwise if you would rather use that.
However, I think that this simple C# class can do everything you're trying to do, in one file. If there are any syntax issues in the code, I guess I'll leave it up to you to solve them, so you can learn on your own.
Label the file you copy and paste the code into as Blast.cs
using UnityEngine;
using System.Collections;
public class Blast : MonoBehavior
{
// Store local private boolean
private bool inArea;
public void Start()
{
// At the start, set boolean to false
this.inArea = false;
}
public void BlastDistance()
{
// On BlastDistance call, set boolean to true
this.inArea = true;
}
public void OnTriggerEnter( Collider meow )
{
// Log your boolean before you check inArea
Debug.Log(area.ToString());
if(inArea) {
// If meow is a GameObject and it's name == Player
if (meow is GameObject && meow.gameObject.name == "Player") {
Debug.Log("MEOW");
} else {
Debug.Log(meow.GetType().ToString());
Debug.Log((meow is GameObject) ? meow.gameObject.name.ToString() : "NOT A GAMEOBJECT");
}
}
}
public IEnumerable OnCollisionEnter( Collider col )
{
// Again, check if collider is a gameobject and the name is Player
if (col is GameObject && col.gameObject.name == "Player") {
this.BlastDistance();
yield return new WaitForSeconds (0.1);
Destroy(col.gameObject);
} else {
Debug.Log((col is GameObject) ? col.gameObject.name.toString() : "NOT A GAMEOBJECT");
}
}
}
I don't think I can call BlastDistance() from javascript to c# @tw1st3d
Just like I literally said in my post, this one file will do everything you need it to do, on it's own. The OnCollisionEnter() will trigger your BlastDistance(). Read the code, and you might see what's happening.
@tw1st3d okay sorry I understand but at the moment I'm trying to learn JavaScript and I made this question to learn the problem and the game I'm making is just a test. Thank you for helping and I'm sorry if I wasted your time :)