• 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
0
Question by turbosheep · Jan 03, 2015 at 11:51 AM · variablenull

String Variable From External Script Set To Null?

Alright, so I'm making a BreakOut clone. I'm working on the collision between a powerup and the paddle. The powerup has a global string 'type', which decides the type of powerup. It gets assigned after instantiating the powerup. Thing is, when I access this variable through the OnCollisionEnter in the paddle script, it becomes null, tested with a print. However, if I print the variable after instantiating the powerup, it isn't set to null. Can anyone clear this up for me? This is my code for the script on the paddle object:

 void OnCollisionEnter(Collision other)
     {
         if (other.gameObject.tag == "powerup")
         {
             if (other.gameObject.GetComponent<PowerUp>().type == "Wide")
             {
                 print ("Wide PU!");
             }
             else if (other.gameObject.GetComponent<PowerUp>().type == "Split")
             {
                 print ("Split PU!");
             } 
             //Only the Else gets executed: the print() only displays 'Type: ', indicating the 'type' variable is null?
             else print("Type: " + other.gameObject.GetComponent<PowerUp>().type);
 
             Destroy(other.gameObject);
         }
     }

This is my code for the script on the PowerUp object:

 public string type;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     //This function acts as a class constructor.
     //It may be called after Initializing this GameObject to set proper Power Up behavior.
     public void Initialize(string type)
     {
         //Test the type string given when a brick breaks in BrickBreak script
         switch (type)
         {
         case "Wide":
             //If the type is 'wide', set proper graphical and logical settings
             renderer.material.color = Color.red;
             type = "Wide";
             print ("Spawn Type: " + type); //This displays the type variable just fine!
             break;
         case "Split":
             renderer.material.color = Color.green;
             type = "Split";
             print ("Spawn Type: " + type); //This displays the type variable just fine!
             break;
         default:
             print ("No proper Power Up constructor string input!");
             break;
         }
     }



 

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

2 Replies

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

Answer by turbosheep · Jan 04, 2015 at 12:40 PM

Alright, did not find out why the variable got set to null somehow, but moving some code to the PowerUp script's Awake() fixed it for now.

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 Ekta-Mehta-D · Jan 03, 2015 at 12:43 PM

Hii ..

Try to assign string to variable type from Inspector . And check still u r getting null.

Comment
Add comment · Show 4 · 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 turbosheep · Jan 03, 2015 at 12:48 PM 0
Share

I feel like this could be the issue. Of course, the field of 'type' in the inspector is empty, since the public variable only gets assigned a string when the object is created. I wonder, why doesn't this field get a value when the object gets created and a value is assigned?

avatar image Ekta-Mehta-D · Jan 03, 2015 at 12:52 PM 0
Share

But wen ur initialize function will call ?

avatar image Ekta-Mehta-D · Jan 03, 2015 at 12:54 PM 0
Share

You need to call Initialize in Start(). Then that variable get assigned.

avatar image turbosheep · Jan 03, 2015 at 02:03 PM 0
Share

I could try that, but it leaves me confused. The Initialize function gets called way before the paddle collides with it and checks for its type. When the Initialize function gets called, it prints the type, this proves Initialize got called in time! Why is the type variable suddenly empty when I call it later on through a OnCollisionEnter from the paddle? I don't get the logic.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How do you clear a variable in the inspector? 4 Answers

if syntax for blank variable. 3 Answers

Setting a variable to nothing 1 Answer

empty component var 1 Answer

Checking if variable is nullable type? 3 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