#why isn't the ball spawning back

1 messages · Page 1 of 1 (latest)

cosmic crystal
#

so i did a simple script where if the ball hits the goal, the ball should spawn back in the 0,0 position. but when i play the game and when the ball hits the goal, it doesn't spawn back, it just stays there.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BallSpawn : MonoBehaviour
{

public GameObject Ball;

 private void CollisionEnter2D(Collision collision)
{
    if (gameObject.tag =="GoalPost")
    {
            Ball.transform.position = new Vector2 (0, 0);
    }
}

}

grave lichen
#

the script, which I assume you've attached to the ball, is comparing the tag of it's own object, not the object that it collided with.

cosmic crystal
#

but isn't it checking for the goalpost tag?

grave lichen
#

no wait I read that the code wrong somewhat. point still stands...

grave lichen
cosmic crystal
#

before i open the project, unity says this:

#

could this be the reason?

grave lichen
#

From what I've seen of your problem so far: No, definitely not.

cosmic crystal
#

alright, i'll try my best to look further into it, but now my brother wants the pc

#

thank u for your help tho :))

grave lichen
#

That's a memory failure right there. Before you go, How many programs do you have open on your pc right now? And how big is your project?

#

Alright then. see you aroundwave

cosmic crystal
grave lichen
#

Hmm, the only other thing I can think of at the top of my head is a memory leak. But c# is a language with G.C. and I can't imagine a newbie working with things like events or delegates yet...

#

Wait you said that this happened before even opening the project? But its tiny? That is very odd.

#

You didn't try to open the project when it was already open and running, did you?

cold frost
# cosmic crystal so i did a simple script where if the ball hits the goal, the ball should spawn ...

Multiple things could be the cause of the problem.

I assume this script is attached to the ball, as it is the one checking for collision, and it isn't the post (or shouldn't be at least).
In this case, you should really just be replacing Ball, with this (this.transform.position or transform.position)

This eliminates the possibility of the Ball not being referenced correctly.

Perhaps your collider isn't setup correctly though. As it is CollisionEnter2D it can't be a trigger collider.

To find out the issue easily, just debug your code. Set a Debug.Log("Message"); at the start of the CollisionEnter2D() to see if it's even being triggered.

zenith rampart
#

Using gameObject will get the tag of the object the script is on. If you use collision.gameObject, you will get the tag of the object that collided with the object with this script (ex. 'A' has the script and A collides with 'B', gameObject checks A's tag while collision.gameObject checks B's tag). Also, you want to use collision.gameObject.CompareTag("GoalPost") rather than collision.gameObject.tag == "GoalPost" as the CompareTag method of checking tags is faster.

halcyon rampart
grave lichen
#

@halcyon rampart I dunno man, I’ve been operating under the assumption that it was a RAM issue. Because how else could OP get a message like this the moment they tried to open up the program. Looking at it again though, 1 GB seems way too big for it to be RAM.

grave lichen
#

Hold up a minute. Creating more assets in RAM generates more data to save that's just common sen-. so... @cosmic crystal, instead of the programs currently open, can you go to your file explorer and check how full your Hard Drive / SSD is?

cold frost
cosmic crystal
cosmic crystal
#

the script i came up with after your notes is this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BallSpawn : MonoBehaviour
{

 private void CollisionEnter2D(Collision collision)
{
    Debug.Log("collision yay");

    if (collision.gameObject.CompareTag("GoalPost"))
    {
            this.transform.position = new Vector2 (0, 0);
    }
}

}

#

and it DOESN'T log when there's a collision, that makes it clear that it is the problem but i'm not sure of what's the fix..

#

(and doesn't respawn)

cold frost
#

Does both objects have a collider? Are both colliders non-triggers?

cosmic crystal
#

yeah

cold frost
#

Zenvin posted about this in FAQ.

My OnCollision.../OnTrigger... events are not invoked!
Unity's collision and trigger events are physics-based. Because of this, there needs to be at least one Rigidbody involved in any collision you want to be detected. For trigger events, it does not matter whether that Rigidbody is kinematic.
Collision events on the other hand must involve at least one non-kinematic Rigidbody.
If you want a full listing of all Rigidbody/Collider configuration combinations, and whether they will cause the events to be invoked, have a look at the very bottom of the Unity Manual for the Colliders: https://docs.unity3d.com/Manual/CollidersOverview.html

Furthermore, it is crucial - as always - that your method signatures are valid. In other words:
- All Unity event methods mentioned in the following need to return either void or IEnumerator.
- OnTrigger... events need a Collider parameter: OnTriggerEnter(Collider col) | OnTriggerStay(Collider col) | OnTriggerExit(Collider col)
- OnCollision... events need a Collision parameter: OnCollisionEnter(Collision col) | OnCollisionStay(Collision col) | OnCollisionExit(Collision col)
- OnTrigger...2D events need a Collider2D parameter: OnTriggerEnter2D(Collider2D col) | OnTriggerStay2D(Collider2D col) | OnTriggerExit2D(Collider2D col)
- OnCollision...2D events need a Collision2D parameter: OnCollisionEnter2D(Collision2D col) | OnCollisionStay2D(Collision2D col) | OnCollisionExit2D(Collision2D col)
cosmic crystal
#

i made sure of everything written here too, still doesn't work

cold frost
#

Hmm, and this script is attached to the ball, right?

cosmic crystal
#

yep

#

and not to the goalpost

cold frost
#

Could you show me both of your collider components. For the goalpost and the ball

cosmic crystal
#

this is for the ball

#

this is for the goalpost

cold frost
#

Hmm, that's odd... To be honest I have no clue. And the objects are really colliding?

cosmic crystal
#

it doesn't even log it too

cold frost
long fractal
#

The method signature here <#1109525606489669642 message> is not valid.
It's using Collision as parameter type for a OnCollisionEnter2D method. Not to mention that the method name is missing the On prefix.

cosmic crystal
#

YES! it worked

#

the reason i removed the "On" from CollisionEnter2D() was because in the previous script i did where i used the same method, unity gave me an error saying that it should be written without the On

#

so i thought it was a new update, like a change for the method

#

but i guess not :)

#

thank you @cold frost @long fractal @grave lichen

cold frost
#

Oh, how did I miss that xD.

halcyon rampart
#

low disk space

grave lichen
grave lichen
grave lichen
#

This is unrelated to you original problem in the code: If your project gets much bigger, you might not have a lot of room on your computer to save any new data or install new packages (to say nothing of any other files or apps that you'd want to save or download). That's what that warning Unity threw was about (thanks nt314p for helping me see that). I would save up some money for an external hard drive and a few USB sticks or something if I were you; 96 gigs is tiny blobsweat holy crud! With a disk that small, I can't imagine there's a lot stuff you'd want to delete or uninstall.

cosmic crystal