- Home /
Problem getting information out of colliding objects
Hello, I have a scene where there are many objects of different size. The plays runs around bumping into them. I need a different reaction if the objects are larger or smaller. Calculating their size and player's size is no problem, I tested and saw they all have a different value. but when I get to the collision test, I get stuck on the same piece of data from some object, and it is not re-checking the data for all objects.
This is my collision code:
function OnCollisionEnter(hit : Collision)
{
if(hit.gameObject.tag == "object")
{
var colliderSize = hit.gameObject.GetComponent(ObjectControl).objectSize;
if (colliderSize < playerSize)
{
Destroy(hit.gameObject); // if object is bigger, change here to push the player or some type of feedback + sound
playerSize +=1;
print("Yummy! I ate a " + colliderSize+ " and my size is now: " + playerSize);
}
else
{
Destroy(hit.gameObject); // if object is bigger, change here to push the player or some type of feedback + sound
playerSize -=1;
print("I've been hit! " + colliderSize + " by a " + colliderSize);
}
}
}
This is the bit of code from each object. They change colour depending if they are smaller or larger than the player's current size:
static var objectSize = 1.0;
var swapMat : Material;
var originalMat : Material;
function Update ()
{
var Player = gameObject.Find("player");
var PlayerSize = Player.GetComponent(PlayerInteractions).playerSize;
objectSize = (transform.localScale.x);
if (objectSize > PlayerSize) //object is larger than player
{
// paint object red
renderer.material = swapMat;
}
if (PlayerSize > objectSize ) //if smaller than player
{
//paint object green
renderer.material = originalMat;
}
}
When you inspect the objects in the inspector at runtime, are the object sizes actually different? Are you actually getting different objects every hit? If so, how?
Yes, the objects sizes are different. I'm adding the bit of code from each object above. They change colour depending wether they are smaller or larger than the player's current size, and that part works fine.
I guess that only leaves the hit
being wrong - where does that come from?
After more testing, it appears it always gets stuck on a certain object, same one every run, and until the player collides with it (and destroys it). it takes the data from it. Afterwards it gets stuck on another object's data.
Answer by aldonaletto · Dec 01, 2011 at 12:29 AM
You should remove static from the objectSize declaration: a static variable is unique, no matter how many instances of the script where it's declared exist - and you must have an independent objectSize variable in each object's script. Being static, each object will modify its value in Update, and only God knows which size the player will see when colliding to an object.
Another suggestion: find the player only once at Start - Find is a slow operation, and may reduce the framerate:
var objectSize = 1.0; // <- remove the static keyword var swapMat : Material; var originalMat : Material; private var Player : GameObject; // make Player a non-temporary variable
function Start (){ // find the player only once at Start Player = gameObject.Find("player"); }
function Update (){ var PlayerSize = Player.GetComponent(PlayerInteractions).playerSize; objectSize = transform.localScale.x; ...
I guess i used my blind eye when reading that... Good catch. By the way, why not cache the reference to the component?
I see! Thank you, been breaking my neck on this. And thanks for the extra enlightenment :)
@gfr gave a good suggestion, @zinnfandel - ins$$anonymous$$d of storing the GameObject "player", you could cache the script reference:
...
var original$$anonymous$$at : $$anonymous$$aterial;
private var playerScript: PlayerInteractions;
function Start(){
var player = GameObject.Find("player");
playerScript = player.GetComponent(PlayerInteractions);
}
function Update (){
var PlayerSize = playerScript.playerSize;
...
Your answer
Follow this Question
Related Questions
Change NavMesh Speed after collision/trigger just for 1 object 1 Answer
How to activate parent camera of gameobject -1 Answers
Calling methods on other Game Objects 1 Answer
Variables from one script to another. 3 Answers
acting on other object during collision: SendMessage or GetComponent? 1 Answer