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
0
Question by Elvis Miron · Nov 22, 2013 at 09:21 PM · variablechildclassdeclareclass library

C#: Declaring a script as a variable

 public class WallProperties : MonoBehaviour
 {
     public Door door;

     public bool IsDoor(bool enable) //enable=true
     {
         if(enable)
         {
             print (enable); //prints true
             door=new Door();
             door.DoorStart(); //sets values to default
             print(door.GetStyle ()); //prints current value
             print ((door==null)?"True":"False");  //prints true
         }
         return(door==null); //returns true
     }
 ...
 }

  • I am trying to have the 'door' variable not NULL
  • The mono class is not attached to anything
  • It gave me lip when the "new Door()" was removed
  • With the "new Door()" I am able to access the Door class

However, the variable 'door' stays NULL. Is there a way that the 'door' variable can be declared and not be NULL or should I just have a boolean class variable for the door?

Comment
Add comment · Show 2
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 · Nov 23, 2013 at 01:00 AM 0
Share

If door is derived from $$anonymous$$onobehavior, then it will need to be attached to a game object...either one you create dynamically or one you create in the editor. Then you will use GetComponent() on the game object with the script to initialize 'door', or you can use drag and drop in the inspector.

avatar image jovino · Nov 23, 2013 at 02:31 AM 0
Share

I,m curious.... how is door equals NULL and is not crashing here?

   door.DoorStart(); //sets values to default
   print(door.GetStyle ()); //prints current value

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by salex100m · Nov 23, 2013 at 01:33 AM

you need to properly instantiate your Door if it is an object. Or you can add the class as a component and then create a reference to that component.

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 jovino · Nov 23, 2013 at 02:34 AM

When you are not attaching your class to a gameobject you need to use ScriptableObject.

http://docs.unity3d.com/Documentation/ScriptReference/ScriptableObject.html

A class you can derive from if you want to create objects that don't need to be attached to game objects.

This is most useful for assets which are only meant to store data.

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

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

C# Custom Class - Declare Variable's Value 1 Answer

Getting a Trigger's parent 1 Answer

Access class variables of selected object 1 Answer

How can I declare a variable as the 100% of another variable? 0 Answers

Can I access variables of scripts that inherit from abstract classes? 2 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