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 Nk.Andrei · Aug 12, 2013 at 01:09 PM · transformclasswarningconstructor

Class transform "null" keyword error.

If my class is attachet to an object and extends Monobehaviour and set a transform from another script from another object I always get the "You are trying to create a MonoBehaviour using the new keyword.This is not allowed" warning....any clues on how to solve this ? the scripts actually work but I want to know what the warning means and what side effects it has.

the class script

pragma strict

class aClass extends MonoBehaviour {

 var pos : Transform;
 
 
 function aClass(a : Transform)
 {
     pos = a;
 }

 function Updater()
 {
     if(pos != null)
     {
         Debug.DrawRay(pos.position,Vector3(0,10,0), Color.red);
         print("abba");
     }
 }

}

and the second script

pragma strict

var grid : aClass;

function Start () {

 grid =  aClass(gameObject.transform);
 

} function Update() { grid.Updater(); }

the warning appears because the transform in the second script and points at the constructor in the class script.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Xtro · Aug 12, 2013 at 01:37 PM

In unity, don't use class constructors unless you are developing an editor extensions. Always put your initialization code into Start event. If you need parameters for initialization, then write an init method with the parameter and call that method after you create the object. It should be something like that (C#)...

 var object = (GameObject)Instantiate(<The prefab object you want to create>);
 grid = object.GetComponent<aClass>();
 if (grid) grid.Init(gameObject.transform);

--- BUT ---

If you want to create and use your own class just like a regular class (you won't attach it to any gameobject in the inspector or in the code) ... you shouldn't extend it from MonoBehaviour.

In that case, just remove the "extend MonoBehaviour" part from your code and it's ready to go.

Comment
Add comment · Show 5 · 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 Nk.Andrei · Aug 12, 2013 at 02:53 PM 0
Share

Yes but if I delete the constructor with the class extending $$anonymous$$onobehaviour and assign the transform this way grid.pos = gameObject.transform I recieve a NullReferenceException a the transform assignment.

avatar image Xtro · Aug 12, 2013 at 04:42 PM 0
Share

Try to debug to find which variable is null. $$anonymous$$y guess is grid is null.

did you try my code sample ?

avatar image Nk.Andrei · Aug 12, 2013 at 07:23 PM 0
Share

Can you please write another sample in JavaScript? I get stuck in c# at the moment......still learning

avatar image Xtro · Aug 13, 2013 at 01:27 PM 0
Share

Sorry. I'm not proficient in Unity's javascript. :(

avatar image Xtro · Aug 13, 2013 at 01:27 PM 0
Share

if you have a problem with C# script, please post it here

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

16 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

Related Questions

How do i edit a constructor array in the inspector? 1 Answer

Can i set class vars automatically when calling class constructor with attributes? 0 Answers

new Monobehaviour as variable returns warning... 1 Answer

How can I instantiate a object with a constructor? 3 Answers

Finding class where current GameObject is assigned to as a var 1 Answer


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