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 /
This question was closed Nov 29, 2013 at 10:48 PM by clunk47 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Borzi · Nov 29, 2013 at 07:07 PM · variable

Put a script as a variable in C#?

I know I have been posting a LOT of conversion questions here and I apologise for that. I was wondering how to write this in C#, anybody know how to convert this?

 var cloneScript : NameOfScript ;

Thank you in advance!

Comment
Add comment · Show 2
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 clunk47 · Nov 29, 2013 at 08:50 PM 2
Share

Well the above answers are correct for the most part, but you also need to reference them with GetComponent (Unless they're public and assigned via inspector. Here's an example. Say you have a script simply named "Script1", and in Script1, you want to call another component attached to the same object as Script1, let's name this second script "Script2". Say in Script2, you have a variable you want to call. This variable must be public. For example:

 public int count = 0;

So first, I'll give you an example of what Script2 would look like:

 //Script2.cs
 
 using UnityEngine;
 using System.Collections;
 
 public class Script2 : $$anonymous$$onoBehaviour 
 {
     public int count = 0;    
 }

Now say you want to change "count" to say, 250, but you want to do this via Script1. Here's an example of how Script1 would look:

 using UnityEngine;
 using System.Collections;
 
 public class Script1 : $$anonymous$$onoBehaviour 
 {
     Script2 script2;
 
     void Start()
     {
         script2 = GetComponent<Script2>();
         script2.count = 250;
         print (script2.count);
     }
 }
 
 
avatar image Borzi clunk47 · Nov 29, 2013 at 10:24 PM 0
Share

This is a great explanation, thanks for your input!

2 Replies

  • Sort: 
avatar image
2
Best Answer

Answer by CoalCzar · Nov 29, 2013 at 07:51 PM

In C# this would be

 NameOfScript clonescript;
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
2

Answer by GhandiJones · Nov 29, 2013 at 08:07 PM

 Public NameOfScript cloneScript;
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

Follow this Question

Answers Answers and Comments

20 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 avatar image avatar image

Related Questions

Enemy Health Problems 1 Answer

Is it possible to change a variable, into a script not assigned to any game object? 3 Answers

How to modify variables from scripts in standard package? 2 Answers

Declaring animations on a script in the editor 1 Answer

How do you retrieve a variable from another script? 0 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