- Home /
no way to understand this
Hello every one, im facing a problem which is senseless. Im very new in unity so maybe i forgot something.
I got this:
using UnityEngine;
using System.Collections;
public class PaddleScript : MonoBehaviour {
float paddleSpeed = 20f;
public GameObject ballPrefab;
GameObject attachedBall = null;
// Use this for initialization
void Start () {
//Instanciar la nueva bola
SpawnBall();
}
void SpawnBall(){
if (ballPrefab == null){
Debug.Log ("No se seleccionado el prefab del GameObject");
return;
}
attachedBall = (GameObject)Instantiate( ballPrefab );
}
// Update is called once per frame
void Update () {
getController();
}
void getController(){
transform.Translate (paddleSpeed * Time.deltaTime * Input.GetAxis("Horizontal"),0,0);
if (attachedBall){
Rigidbody ballRigidBody = attachedBall.rigidbody;
ballRigidBody.position = transform.position + new Vector3(0,0.75f,0);
if (Input.GetButtonDown("LaunchBall")){
ballRigidBody.isKinematic = false;
ballRigidBody.AddForce(300f ,300f,0);
attachedBall = null;
}
}
}
void OnCollisionEnter (Collision col){
foreach (ContactPoint contact in col.contacts){
if ( contact.thisCollider == collider ){
float english = contact.point.x - transform.position.x;
contact.otherCollider.rigidbody.AddForce( 300f * english, 0 , 0);
}
}
}
}
It's supposed to work (and in fact it does), BUT every time i run the game, console shows the message "No prefab selected in inspector" which i've set up for testing purposes. Actually prefab it's set up in the inspector, so, what's going on?
Ow i almost forgot it, in the line:
attachedBall = (GameObject)Instantiate( ballPrefab );
monodevelop says that the ballPrefab doesn't exists in this context
Thanks in advance!
Any chance you have the script attached to more than one game object?
Answer by lostcitizen · Jul 04, 2013 at 10:58 PM
OK guys, I got it! you were COMPLETELY RIGHT! I had my script attached to two game objects just like robertbu and Eric5h5 said.
Thank you very much, I apreciate your help!
Answer by Slobdell · Jul 03, 2013 at 11:12 PM
// At the top you make this null
GameObject attachedBall = null;
// SpawnBall method
void SpawnBall(){
// ballPrefab is a prefab, not a variable. This should be "attachedBall"
// attached ball is still null, so this will return true
if (ballPrefab == null){
// You log
Debug.Log ("No se seleccionado el prefab del GameObject");
// You return for some reason, this ends the method. Remove this
return;
}
// This never happens because method already returned
attachedBall = (GameObject)Instantiate( ballPrefab );
}
It seems you don't understand even one line of this code ;)
He sets his "attachedBall" variable null because at the beginning there is no instantiated object so the variable doesn't reference anything.
ballPrefab is a variable and is set up in the inspector to reference a prefab, the prefab that should be instantiated
He checks if the ballPrefab variable has been setup and if it's null (not assigned) he prints a warning and exits the function because when the prefab variable is null you can't instantiate it.
The code is absolutely fine. It has to be something else
Bunny, you're confused. You can't just create a gameobject and think it will magically "link" up to something in the inspector. You have to instantiate a prefab. This block of code will never instantiate anything because ballPrefab will forever be null, so attachedBall will never get created. If there is a gameobject out there, you have to find it and attach it to this variable manually. Sure would be neat if Unity just knew what you wanted it to do without having to tell it. This code is absolutely not fine. Look at the error code, ballPrefab doesn't exist in the current context...that means it doesn't exist. Any questions?
Nope, Bunny83 is not confused. It will indeed "magically" link up to something in the inspector, if "dragging and dropping" is now considered "magic". ;) I think you missed something really basic about how Unity works. To say "ballPrefab will forever be null" is quite wrong; it won't be null because you'll assign it in the inspector using a prefab in your project view.
robertbu's comment is correct as usual: lostcitizen has the script attached to more than one object (and has neglected one of the objects). That explains why it works fine and yet generates an error, because one instance of the script has the prefab linked in the inspector and the other instance does not.
Guys I don't mean to be an A -hole here so please $$anonymous$$ch me something, it's highly probably that I'm missing something ridiculously simple. How do you drag and drop a prefab onto a script variable?
The benefits are:
1) only assets that are directly referenced by public variables are included in a build, as opposed to the Resources folder, where everything you put in there is included whether it's referenced or not.
2) using public variables allows you to completely disassociate your code from the assets, so you can move them around, rename them, etc. without having to remember to rewrite your code to match, which results in fewer potential bugs.
3) not using strings means that it's impossible to have typos when referencing objects (although you can have the issue of forgetting to link up a public variable, as demonstrated by this question).
4) different instances of the script can reference different prefabs, which is impossible if you're hard-coding things using Resources.Load.
Resources.Load does have certain uses, but in most cases I'd recommend avoiding it because of these issues.
Then you don't have to check for null.
There's just as much reason to check for null when using Resources.Load, since there are any number of reasons why the object you're trying to load might not exist, as explained above.
Your answer
Follow this Question
Related Questions
Shields 'always' inactive. Player object doesn't recognise if child exists(Solved) 1 Answer
Creating a 'rotation' system 1 Answer
How do I create a JavaScript class that creates an object with viewable variables in the inspector? 3 Answers
Why is my class hidden in inspector with C# and not JS? 1 Answer
Mask field in the editor 8 Answers