Hello,
I’m trying to make another block breaker from scratch (graphics stolen from web ) I have just one issue with the ball. The ball is not moving according to paddle. Unity gives “Disc (the ball) cannot take parameters” error.
The error shown here is that after first ‘difference’ declaration you redeclare it in Start (sorry I don’t find the good words to explain)
you should have :
public Vector3 difference;
void Start() {
difference = new Vector3();
}
and not Vector3 difference = new Vector3();
if you look at the last ss there’s a declaration for “difference” vector before Start function
You are trying to pass parameters into the Update
method. This is a Unity method which doesn’t accept parameters, line number 20 in your second screenshot.
Incidentally, it’s a lot easier to copy/paste your code into the forum, especially for those trying to help you, as they can equally copy/paste parts of your code.
Regarding @Dieedi’s reply, you are re-declaring difference
on line 14 in the second screenshot - remove the Vector3
before difference
.
See also;
- Forum User Guides : How to apply code formatting within your post
You had warned me, sorry What can I do to use parameters?
Why do you want to use parameters?
(no problem btw)
I wanna make disc always on my paddle like our course, at the course we declared a vector computes distance between ball (in my situation disc) and paddle and keep the ball always at that distance. So I need computer compute this vector. Yes I can calculate it myself but it’s not that cool you know
Instead of trying to pass the Rigidbody in to the method, you could make it an instance variable at the top of the class. The find the component in the Start
method, you would then be able to access it within the body of the Update
method because it will be available to all methods in the class.
As an example;
public class ExampleClass : MonoBehaviour
{
private Rigidbody2D rigidBody; // instance variable
private void Start()
{
rigidBody = gameObject.GetComponent<Rigidbody2D>(); // assumes a little bit too much, e.g. a Rigidbody2D will definitely be attached
}
private void Update()
{
// can now access rigidBody here without passing it in
Debug.Log(rigidBody.ToString()); // I am spamming your console here
}
}
By the way, I think one of your screenshots is actually missing from you first post, there is a little bit of text in between them which suggests the cursor was half way through the text when you uploaded the last image and it over-wrote it.
Thank you Rob, you’re awesome again. and I made a mistake while trying this and I think it will be better than any other block breaker game thanks to mistake. I won’t forget your warning again by the way
You’re very welcome Muhammed.
It’s no really a warning, just a little advice to help both yourself and also those that help you - much easier to be able to copy/paste chunks of code around in replies which of course you can’t do with a screenshot.
All the best
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.