Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by Kith · Jul 05, 2011 at 10:19 PM · c#referencingcs0246outside

C# Referencing other Script

Hello again, I'm very new to C#, but desperately want to start learning. However, I'm having some trouble with something that I'm pretty sure is very simple.

In Javascript, I could easily do this...

 private var GameGod : GameObject; 
 private var GameScript : GodScript;

 function Start() {
     GameGod = GameObject.Find("Game God");
     GameScript = GameGod.GetComponent("GodScript");
 }

However, when I try to do something similar in C#

 private GameObject GameGod;
 private GodScript GameScript;

   
  void Start () {
         GameGod = GameObject.Find("Game God");
         GameScript = GameGod.GetComponent("GodScript");
     }

I get an error saying "The type or namespace name "GodScript" could not be found. Are you missing a using directive or an assembly reference?"

I'm not sure what's wrong, but I have a feeling I'll feel stupid when I find out lol.

Comment
Add comment · Show 8
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 Kith · Jul 05, 2011 at 10:32 PM 0
Share

P.S. GodScript was written in Javascript. I don't know if that's relevant or not

avatar image sharat · Jul 05, 2011 at 10:34 PM 0
Share

That definitely has something to do with it. I remember reading something about the compile order of Javascript and C# on the docs somewhere, but there can be problems when you mix the two.

avatar image Kith · Jul 05, 2011 at 10:36 PM 0
Share

$$anonymous$$eh...I'll just rewrite this script in Javascript I guess. That's what I get for wanting to learn C# halfway through a game project lol.

avatar image sharat · Jul 05, 2011 at 10:38 PM 0
Share

Here it is.

http://unity3d.com/support/documentation/ScriptReference/index.Script_compilation_28Advanced29.html

I would say it probably is easier to start with independent scripts, but you can affect the order things are compiled by changing the location. Otherwise you can't have C# dependent on JavaScript or vice-versa.

avatar image Chris D · Jul 05, 2011 at 10:38 PM 0
Share

Is your GodScript type being defined anywhere the script can find it? If you're mixing and matching languages, take a look here to see what you have to worry about in terms of compile order.

edit: ya, what @sharat said

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by flaviusxvii · Jul 06, 2011 at 08:17 PM

First and foremost, try not to start your variable names with upper case letters. It makes it hard to distinguish them from types.

You probably need to do this:

 gameScript = gameGod.GetComponent<GodScript>();
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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

is it possible to give only part of array through [] 1 Answer

how to set up interaction method using raycast? 1 Answer

Optimising references to other scripts 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