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 akashsunny · Dec 08, 2013 at 07:35 AM · referencewarningsmono-behaviour

Monobehaviour warning for new keyword

 //class name - Vectors()
 
 #pragma strict
 var target : Rigidbody;
 private var Dir : Vector3;
 Dir = transform.position;
 private var px : Vector3=  Vector3(2,2,2);
 function Update () 
 {
 
 if(Input.GetMouseButtonDown(0))
 {
 Dir.y = target.position.y + px.y;
 target.position = Dir;
 }
 if(Input.GetMouseButtonDown(1))
 {
 Dir.y = target.position.y - px.y;
 target.position = Dir;
 }
 }



 //class name - SimpleMovement_circular
 
 #pragma strict
 
 private var z: float;
 private var y :float;
 var x : float = 5;
 private var PosRef : Vectors = new Vectors();
 var target : Rigidbody;
 var rotationSpeed : float;
 private var i : int = x;
 
 function Awake() 
 {
 
 }
 
 function Update () 
 {
     for(i=0 ; i<x ; i++)
     {
     y = Mathf.Lerp( 0+i , i+1 , rotationSpeed*Time.deltaTime);
     z = Mathf.Lerp( 0+i , i+1 , rotationSpeed*Time.deltaTime);
     }
 }

warning shown for the second script ---

You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all UnityEngine.MonoBehaviour:.ctor() Vectors:.ctor() (at Assets/Scripts/Vectors.js:1)

what could possibly be the problem and hoe could i create a reference to the script named Vectors In the other script....could the reference be setup in the awake or the start functions.

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

Answer by Benproductions1 · Dec 08, 2013 at 07:58 AM

Hello,

In Unityscript, when a script is compiled, the contents of that script which are not in a class will be automatically placed in a class named after that of the file and will automatically inherit from MonoBehaviour. MonoBehaviour objects cannot be constructed by the usual means, because of how they work with the Unity Engine.

If you want to make a class that doesn't inherit from MonoBehaviour you should learn how the class keyword works :)

Hope this helps,
Benproductions1

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 Bunny83 · Dec 08, 2013 at 08:17 AM

Sure if both scripts are already in the scene, attached to different objects you can simply drag the gameobject that has the "Vectors" script attached onto the variable PosRef in the inspector. For this the variable has to be public!

If you create the objects dynamically (so both or one of them are Instantiated at runtime) you can setup the reference in Start. There are many ways to get a reference to another object. Keep in mind that every MonoBehaviour has to be attached to a GameObject.

See Accessing Other GameObjects for various ways to access scripts / components on the own gameobject or others.

We can't give you a more detailed answer without knowing how your scene hierarchy looks like and which script is attached to what.

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

Add a reference to non-monobehaviour class to a gameobject 3 Answers

Getting a reference to a class created in a different gameObject (Javascript) 1 Answer

GUI problem: Lack of Events and Listeners. 1 Answer

Default References of a Script 1 Answer

PDF scripting reference? 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