Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
-1
Question by lostcitizen · Jul 03, 2013 at 09:43 PM · inspectorgame object

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!

Comment
Add comment · Show 1
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 robertbu · Jul 03, 2013 at 10:50 PM 2
Share

Any chance you have the script attached to more than one game object?

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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!

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 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 );
     }
Comment
Add comment · Show 7 · 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 Bunny83 · Jul 04, 2013 at 12:06 AM 0
Share

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

avatar image Slobdell · Jul 04, 2013 at 12:23 AM 0
Share

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?

avatar image Eric5h5 · Jul 04, 2013 at 01:49 AM 2
Share

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.

avatar image Slobdell · Jul 04, 2013 at 02:06 AM 0
Share

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?

avatar image Eric5h5 · Jul 04, 2013 at 02:49 AM 1
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 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

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


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