- Home /
differentiate between 3 types of powerups
hi guys i have 3 kinds of powerUps and i want to each of them to do something different for that i used gameObject.Find
but its dosent seems to work(it aint adding points as it should be)
using UnityEngine;
using System.Collections;
public class PowerUpScript : MonoBehaviour {
// GameObject powerUp1;
// GameObject powerUp2;
// GameObject powerUp3;
// Use this for initialization
void Start () {
rigidbody.AddTorque( Vector3.forward * 60f );
// powerUp1 = GameObject.Find ("powerUp");
// powerUp2 = GameObject.Find ("powerUp 2");
// powerUp3 = GameObject.Find ("powerUp 3");
}
void Update()
{
}
void OnTriggerEnter (Collider other)
{
//
PadleScript paddleScript = GameObject.Find ("paddle").GetComponent<PadleScript>();
if (GameObject.Find ("powerUp"))
paddleScript.AddPoints (100);
if (GameObject.Find ("powerUp 1"))
paddleScript.AddPoints (100);
if (GameObject.Find ("powerUp 2"))
paddleScript.AddPoints (150);
Destroy (gameObject);
}
}
and here is my other script where the powerups array is initialized:
using UnityEngine;
using System.Collections;
public class BrickScript : MonoBehaviour {
static int numBricks = 0;
public int pointValue = 10;
public int hitPoints = 1;
public int powerUpChance = 3;
public GameObject[] powerUpPrefabs;
// public GameObject BrickMovePrebaf;
GameObject BrickMove;
// Use this for initialization
void Start () {
numBricks++;
}
// Update is called once per frame
void Update () {
BrickMove = GameObject.Find ("brickMove");
if (BrickMove) {
if (transform.position.x > 7.5f)
rigidbody.AddForce(300*Input.GetAxis ("Horizontal"),0,0);
if (transform.position.x < -7.5f)
rigidbody.AddForce(-300*Input.GetAxis ("Horizontal"),0,0);
}
}
void OnCollisionEnter( Collision col ) {
hitPoints--;
if ( hitPoints <= 0 ) {
Die();
}
}
void Die() {
Destroy( gameObject );
PadleScript paddleScript = GameObject.Find ("paddle").GetComponent<PadleScript>();
paddleScript.AddPoints (pointValue);
numBricks--;
if ( Random.Range(0, powerUpChance) == 0 )
Instantiate( powerUpPrefabs[ Random.Range(0, powerUpPrefabs.Length) ] , transform.position, Quaternion.identity );
if ( numBricks <= 0 )
{
Application.LoadLevel(Application.loadedLevel + 1);
}
}
}
thanks
Answer by fafase · Jan 22, 2014 at 07:35 PM
See this http://answers.unity3d.com/questions/622942/detecting-a-collision-between-specific-two-objects.html
Oky i changed my "if" statemaent to if (col.collider.name == "powerUp") and it didnt worked also i used: Debug.Log (col); to check if my paddle recognize the powerup every time and the log is showing only once
In your example the reference is called other, maybe that is your issue. You should paste your code for us to see.
i changed the name from other to col and vice verse and its aint working quit yet
Here you go using UnityEngine; using System.Collections;
public class PowerUpScript : $$anonymous$$onoBehaviour {
// GameObject powerUp1;
// GameObject powerUp2;
// GameObject powerUp3;
// Use this for initialization
void Start () {
rigidbody.AddTorque( Vector3.forward * 60f );
// powerUp1 = GameObject.Find ("powerUp");
// powerUp2 = GameObject.Find ("powerUp 2");
// powerUp3 = GameObject.Find ("powerUp 3");
}
void Update()
{
}
void OnTriggerEnter (Collider col)
{
Debug.Log (col);
PadleScript paddleScript = GameObject.Find ("paddle").GetComponent<PadleScript>();
if (col.collider.name == "powerUp")
Debug.Log ("1");
if (col.collider.name == "powerUp 1")
Debug.Log ("2");
if (col.collider.name == "powerUp 2")
Debug.Log ("3");
Destroy (gameObject);
}
}
Your answer
Follow this Question
Related Questions
Difference between gameObject.transform and just transform? 1 Answer
Changing a Player's GameObject when damaged/powerup 0 Answers
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
What's the difference between gameObject and transform when using GetComponent 2 Answers
Gameobject Differentiation 1 Answer