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
3
Question by .hooligan · Nov 30, 2010 at 11:29 AM · attach

Attach a script to another script problems C#

I'm have a couple weird problems with attaching scripts inside other scripts.

I have a game object on the stage with the following script.

using UnityEngine; using System.Collections;

public class GameManager : MonoBehaviour {

//Managers
public LevelManager levelManager;

}

And I created the other script which looks like the following

using UnityEngine; using System.Collections;

public class LevelManager : MonoBehaviour {

}

Now when I try to drag and drop the script onto the spot in the scripts variables in the unity GUI it wont attach. Also when I click the little browse circle it wont show up in the list.

I've tried it with multiple scripts and I'm having the same problem.

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 .hooligan · Nov 30, 2010 at 12:43 PM 0
Share

This has me beat. Is their an article somewhere I can read that goes over including other scripts in c#. I've had a search but can't find anything. I must be doing something wrong.

3 Replies

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

Answer by pyro · Nov 30, 2010 at 02:35 PM

You need to attach the script to an object in the scene first, then drag that OBJECT into your other script.. not the script file itself.

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 pyro · Nov 30, 2010 at 02:36 PM 0
Share

or drag a prefab with that script attached

avatar image .hooligan · Dec 01, 2010 at 12:01 AM 1
Share

This worked but I remember not having to attach the script to a Game Object? or has it always been like this?

Would it be better if I just attach the script as a component to the Game$$anonymous$$anager GameObject?

avatar image pyro · Dec 01, 2010 at 06:10 PM 1
Share

yes think of the script file as the class definition, and when you drag it onto a gameobject it actually instantiates the class in memory it doesnt really matter where you attach the component, its personal preference. I generally like to keep separate components on separate gameobjects

avatar image Content1of_akind · Apr 23, 2019 at 08:25 PM 0
Share

Appreciate it, Cheers!

avatar image
1

Answer by Maarten · Nov 30, 2010 at 11:35 AM

Please make sure the LevelManager class has the attribute [Serializable]. Otherwise it wont show in the inspector.

Comment
Add comment · Show 1 · 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 .hooligan · Nov 30, 2010 at 11:47 AM 0
Share

The script shows in the inspector I just cant drag and drop the script on it.

avatar image
0

Answer by ckfinite · Nov 30, 2010 at 02:27 PM

I cannot find a problem in any of your code. I have done just this sort of thing hundreds of times in my project, and it works. Try right clicking on any asset, and selecting "reimport all". I find that fixes a lot.

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

1 Person is following this question.

avatar image

Related Questions

Create an object and assign code to it via script? 1 Answer

scripts as a addcomponent to a gameobject 2 Answers

unable to attach a script to a prefab 1 Answer

Can't Add Script 1 Answer

GO.AddComponent just adds an empty Script 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