- Home /
Object instantiates on top of the previous object
I've written a script where a level is supposed to be instantiated when my character hits a specific location. It works perfectly the first time it's meant to instantiate. But on the second time, it instantiates the level directly on top of the previous level. Here's a video of whats happening: https://vimeo.com/283706809 And here's the code (It isn't all of the code, only the code relevant to this question):
if (collision.tag == "Score2")
{
scoreText.text = (++score).ToString();
Destroy(collision.gameObject);
int randomNumber = Random.Range(0, 2);
if (randomNumber == 0)
Instantiate(course[0], new Vector2(-0.39f, -2f + transform.position.z), transform.rotation);
else
Instantiate(course[1], new Vector2(-0.39f, -2f + transform.position.z), transform.rotation);
return;
Answer by Ellie97 · Aug 07, 2018 at 03:47 PM
This is probably an issue with incorrect parenting and positioning. This may be a better approach:
void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Player")
{
//generate next level piece here
}
}
Alternatively, you can use a trigger and create a trigger box as part of each course which spans the width of the play area. Attach this to each course.
Just tried your code but it made no difference like it completely ignored the code. Here's what I wrote: (I also tried with the tag as Player)
void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Score2")
{
int randomNumber = Random.Range(0, 2);
if (randomNumber == 0)
Instantiate(course[0], new Vector2(-0.39f, -2f + transform.position.z), transform.rotation);
else
Instantiate(course[1], new Vector2(-0.39f, -2f + transform.position.z), transform.rotation);
return;
}
}
What object is this script attached to? And have you tried some Debug.Log()'s to track where it's going wrong?
It's attached to my player. Adding Debug.Log won't do anything, I know it works. It's just a problem with the location it instantiates the course.
Answer by LTonon · Aug 09, 2018 at 01:01 AM
I would deal with this problem by saving the reference of the last instantiated course in a variable, then using its transform to calculate the new position. I'm proposing this solution because your calculation of the next instantiation position seems weird, but feel free to explain to me it if you don't want to change it, maybe it makes sense. Anyway, I would do something like this:
private float courseHeight;
private GameObject lastInstantiatedCourse;
/* You should initialize the last course variable with the first one instantiated for the start of the game */
void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Score2")
{
int randomNumber = Random.Range(0, 2);
if (randomNumber == 0)
lastInstantiatedCourse = Instantiate(course[0], new Vector2(-0.39f, lastInstantiatedCourse.transform.position.y + courseHeight), transform.rotation);
else
lastInstantiatedCourse = Instantiate(course[1], new Vector2(-0.39f, lastInstantiatedCourse.transform.position.y + courseHeight), transform.rotation);
return;
}
}
I did not test it out, but it should work.
Just tried out your code and it gave me this error
Assets/Scripts/ScoreScript.cs(13,19): warning CS0649: Field ScoreScript.courseHeight is never assigned to, and will always have its default value 0
Also, I don't get what you wrote in lines 4 and 5.
Yeah, you were not supposed to just copy and paste my code, I didn't make it for this but to give you a general idea of how to approach the problem hahaha 1 - The error occurs because you never assigned a value to the courseHeight, and I thought you would do it. Anyway, to correct this you have to get the size of the course sprite in the y-axis somewhere of your code before using it. If all your courses have the same size, you can just do "courseHeight = course[0].GetComponent().size.y;" on an Awake method. If your courses can have different sizes, you have to calculate it right before you instantiate the new course on your OnCollisionEnter method. Again, I did not test it out since I'm answering on the cellphone and it's just to give you a general idea.
2 - Lines 4 and 5 are my comment? Well, I meant to say that you have to initialize the "lastInstantiatedCourse" variable with some value before using it in the OnCollisionEnter method. For that you have to assign the first course GameObject of your game (the one your player begins at) to this variable in the Awake method.
Basically, you should just initialize these two variables that I used as an example with the correct values ^^.
Okay, so I've added the courseHeight = course[0].GetComponent().size.y; (All the courses are the same height). Now for the lastInstantiatedCourse variable, do I assign it with the courseHeight? Or do I make another variable? And please just note that I'm a beginner with C Sharp so I'd appreciate it if you could explain it in a way I can understand.