- Home /
The question is answered, right answer was accepted
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!
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);
}
}
This is a great explanation, thanks for your input!
Answer by CoalCzar · Nov 29, 2013 at 07:51 PM
In C# this would be
NameOfScript clonescript;
Follow this Question
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