- Home /
Coding problem found. Proceeding to continue with tutorial.
When ball activated, 2 balls appear instead of 1
Hi, I'm totally new to this website and I hope this is the right place to post my difficulties. I'm following a unity tutorial where I'm writing a football game. The problem is the instructor is not always responsive and sometimes, some of the questions the students place are just not answered to. My question is in the description given. When I disable the characteristics below the ball and leave the correct one active, 2 balls persistently appear instead of one. I cannot continue with the course as the continuation is only possible if 1 ball appears without rolling. How can i fix this? I cannot insert the picture as there is a parsing problem. My file is .jpg.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameController : MonoBehaviour
{
[SerializeField]
GameObject ballPrefab;
/*
[SerializeField]
Rigidbody rb; */
[SerializeField]
float ballForce;
GameObject ballInstance;
Vector3 mouseStart;
Vector3 mouseEnd;
float minDragDistance = 15f;
float zDepth = 25f;
// Start is called before the first frame update
private void Awake()
{
}
void Start()
{
CreateBall ();
}
// Update is called once per frame
void Update()
{
if(Input.GetMouseButtonDown(0))
{
mouseStart = Input.mousePosition;
}
if (Input.GetMouseButtonUp(0))
{
mouseEnd = Input.mousePosition;
if(Vector3.Distance(mouseEnd,mouseStart) > minDragDistance)
{
//throw ball
Vector3 hitPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, zDepth);
hitPos = Camera.main.ScreenToWorldPoint(hitPos);
ballInstance.transform.LookAt(hitPos);
ballInstance.GetComponent<Rigidbody>().AddRelativeForce(Vector3.forward * ballForce, ForceMode.Impulse);
}
}
}
void CreateBall()
{
ballInstance = Instantiate(ballPrefab, ballPrefab.transform.position, Quaternion.identity) as GameObject;
}
}
Answer by tormentoarmagedoom · Jul 11, 2019 at 01:45 PM
Hello.
If 2 balls are instantiated is because you are doing it twiice, somewhere, somehow, itds been called twice, or there are 2 objects with the same scrip, or something like that.
You MUST debug the code while running, and cheking when the function to create balls is called.
Bye
Answer by Beginner_at_Unity · Jul 11, 2019 at 02:54 PM
I cannot paste anything here but I think I can share the link. https://drive.google.com/file/d/1-Gx1mcoJwsBCxMMdYV7wEBMNEf4X4L9z/view?usp=sharing. I cannot find where the fault is. I have reviewed throught the tutorial and there's only ONE script. You can download and check.
Answer by I_Am_Err00r · Jul 11, 2019 at 04:00 PM
My best advice is to never instantiate objects, just set them active and inactive.
void CreateBall()
{
ballPrefab.gameObject.SetActive(true)
ballPrefab.transform.position = whereverYouAreTryingToInstantiateTheBall
}
I don't see anywhere you need to destroy the ball, so that might be why you are seeing two. Whenever you need to destroy the ball (which is typically done when you instantiate something) just type in this line of code wherever the destroying needs to happen:
ballPrefab.gameObject.SetActive(false)
Is it due to differences of version or a built-in bug? So why previous code given by tutorial cannot work?
It's not that it doesn't work, and don't think in the early stages of coding that there is only one right way to do anything; there are many many different right ways to do something. I find that it's very easy to Instantiate something, but it's hard to manage it after that and I too run into issues that I don't foresee immediately.
If you only have one football in the game, it would be appropriate to just bring that football into every scene (there are ways to do this with code (Singleton), but for your game just remember to add it in every scene manually) and then move the object in the scene to the positions you are trying to instantiate at.
How do you move? if you type something like this:
ballPrefab.transform.position = theNewPosition;
It will instantly snap to whatever you decide theNewPosition is, but there are a ton of more elegant ways to move the ball, and if they haven't already gone over that in the tutorial you are taking, I'm sure they will cover that later.
For now, ins$$anonymous$$d of doing an instantiation, just manually put the football gameObject into every scene. Add this to the list of your variables:
[SerializeField]
GameObject ballPrefab;
/*
[SerializeField]
Rigidbody rb; */
[SerializeField]
float ballForce;
GameObject ballInstance;
Vector3 mouseStart;
Vector3 mouseEnd;
float $$anonymous$$DragDistance = 15f;
float zDepth = 25f;
public GameObject ballPrefab;
That should give you an option on that GameController script to add something, if you have your football inside your scene, drag it from the hierarchy into the slot in the inspector that you see now; this will be the reference to the football.
Finally, this code will "instantiate" an "destroy" the ball when you need to (if they get to that in your tutorials):
ballPrefab.gameObject.SetActive(true); //This is spawning the ball
ballPrefab.gameObject.SetActive(false); //This is destroying the ball
Let me know if you have any questions.
Thank you for the information. I will stop here temporarily. Now that your earlier codes worked, i am working on the next objective. I actually sent some information to the unity development $$anonymous$$m because everytime I follow the tutorial, something goes wrong and i suspect it's some bug. I have found some bugs but not everything is related to unity. Some are related to the flow of the fbx file the tutorial gave. Now I am giving the link for both the pictures I sent to them and the entire project so you and anyone else can play and understand the problems i have. I cannot move the ball to "kick" the ball. Check the project and the pictures I have. The link to the zip file is here. Check if you understand and can help solve the problem. The black lines in the picture is where you use the mouse - press the left mouse button without lifting and move (kick) the ball to the directions you desire. The tutorial shows it should work but $$anonymous$$e don't. Why?
Note that this link doesn't require any passwords. I have put the picture in another folder because it cannot be uploaded due to some silly parsing error.
Follow this Question
Related Questions
My Instantiated object is affected by the original one 1 Answer
Move ball inside pipe like tunel 1 Answer
How to hit the ball? 1 Answer
Animate a bouncing ball. 1 Answer