Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jul 22, 2019 at 04:17 AM by Beginner_at_Unity for the following reason:

Coding problem found. Proceeding to continue with tutorial.

avatar image
0
Question by Beginner_at_Unity · Jul 11, 2019 at 01:38 PM · ballgamestwo

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;
 
     }
 
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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)
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Beginner_at_Unity · Jul 12, 2019 at 06:33 AM 0
Share

Is it due to differences of version or a built-in bug? So why previous code given by tutorial cannot work?

avatar image I_Am_Err00r Beginner_at_Unity · Jul 12, 2019 at 01:34 PM 0
Share

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.

avatar image Beginner_at_Unity I_Am_Err00r · Jul 12, 2019 at 02:21 PM 0
Share

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.

https://drive.google.com/file/d/1wNEWRnDcFTyDQvez73IOSaGUTzY$$anonymous$$X$$anonymous$$N2/view?usp=sharing

Show more comments

Follow this Question

Answers Answers and Comments

108 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Click Two objects At Once Windows 7 Touch 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges