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 melonman · Sep 08, 2013 at 03:04 PM · instancesaccessing

How to declare and access other scripts publicly in Csharpe?

How can I access other scripts publicly in Csharpe?

I want to be able to reference a script from another script and then cache that so that I am able to just call it up whenever.

How would I declare this instance and cache it and use it in the update function?

I have already tried

 void Start ()
 {
 RandomScript randomScript = cube.GetComponent<RandomScript>();
 }
 

However I get an error saying randomScript cannot be found whenever I implement it in update. I know this is where I fall down because I just don't know how to declare a script and this is nowhere in the docs other than scriptableObject and thts not what I want.

This is driving me nuts can anyone help me out here plz?

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

3 Replies

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

Answer by melonman · Sep 08, 2013 at 04:22 PM

I finally found the answer to this.

So to declare an instance of the script you want to access seems to be as follows

  //declare the actual script and then the name you want to 
  //reference it as - usually just make the first letter lower or 
  //upper case in order to keep it clean.
  public ScriptIWantToAccess scriptIWantToAccess;


Then within the "Start" function write the "referenced name" of the script not the actual script first and then use GetComponent to get the actual script as follows...

 scriptIWantToAccess = cube.GetComponent<ScriptIWantToAccess >();
 
 or
 
 scriptIWantToAccess = cube.gameObject.GetComponent<ScriptIWantToAccess >();

Either should be fine however if your putting it on a camera I think it's your camera object then same as up top otherwise it's ".main.gameObject" and the rest as follows in the example for the GetComponent part.

Then finally you just call the instance name you made for the original script in update function or fixed update, OnGui etc and it should give you access to all the original scripts goodies

as follows......

 scriptIWantToAccess.celebrationBanquetAchieved = true;


Hopefully this saves other people from considering headbutting a wall in frustration at this hurdle.

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 dorpeleg · Sep 08, 2013 at 03:45 PM

Make sure your "RandomScript" is a public class.

 public class RandomScript : MonoBehaviour {

Make sure you add the script as a component to the cube.

Make sure your reference to the cube is correct.

If you do all that, your script should work.

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 melonman · Sep 08, 2013 at 04:06 PM 0
Share

Nope that didn't work for me.

I thought exactly the same but it appears not to work that way.

I did find the answer tho finally after looking absolutely everywhere.

Will post that up once I finish typing it up but thank you anyway dorpeleg.

avatar image
0

Answer by Gorgo · Apr 06, 2016 at 03:44 AM

@melonman All of the above should be valid. Also make sure you are including the namespace to the script title, if there is one.

 namespace UnityStandardAssets.Characters.FirstPersonController
 {
     public class FirstPersonController : MonoBehaviour

So in order to call "FirstPersonController" script, we need to declare it's namespace with either of these ways:

 using UnityStandardAssets.Characters.FirstPerson;

or straight at the declaration of our variable

 public UnityStandardAssets.Characters.FirstPerson.FirstPersonController player;

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

17 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

Related Questions

Accessing Variables from another object Script 1 Answer

Script is changing default values 2 Answers

Instantiated cloned GameObject returns Null outside Start method 0 Answers

Adding variables from all scripts 2 Answers

How to access a colliders function or variable 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