|
Hello, can someone please provide a step by step tutorial on c# code on how to access functions in another script I have? I have looked at many posts and code resources but they just give me errors.
(comments are locked)
|
|
Thank you for all the replies. Ok using this code I was able to access the script. However, when trying to access another script attached to another model, it does not work. It does not return errors but my Male Character still shows when the scene starts.
I did find out the reason it isn't working. The script has to be applied to all children and not just the parent. How can I do this if I have a large amount of children?
(comments are locked)
|
|
Correct me if I'm wrong, but the EASIEST way to access another script's functions is if that script is of a static class with object inheritance. Make this script and store it anywhere in your project. You don't need to attach it to a gameobject. Just leave it in a folder. Then, in any script you have in the entire project, you can just call that function by just doing StaticClasses.DebugFunction();
(comments are locked)
|
|
Ok, well ok using this code I can access the other script: But, when using the code later, in another function I get femalenudeScript is unknown error: femalenudeScript.ChangeEnabled(true); You have to GetComponent in each function, or better yet, set a 'global' variable in Start and then use that variable in the other functions.
Dec 20 '11 at 08:28 AM
DaveA
Ok what is the best way to define a global variable. I tried putting public in front but that doesn't work, I tried putting public before the start function, and that also didn't work. When I do actual c# programming, I just define everything before the main function or any function to make it global.
Dec 20 '11 at 04:50 PM
kmccmk9
I suggest you forget about GetComponent and do it the easy way. See the other answer...
Dec 20 '11 at 05:25 PM
jahroy
(comments are locked)
|
|
Here is BY FAR the easiest way to do this: Here is how to access a script named TheOtherScript that is defined in a file named TheOtherScript.cs from another scrip named TheAccessor. TheAccessor.cs:
TheOtherScript.cs:
Now all you have to do is follow these steps to get it to work:
You should see the code word printed in the debug console every frame. By the way... I don't use C#, so I'm sure there are some syntax errors in there. This is just meant to be an example.
(comments are locked)
|

Assuming you read http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html, then you should post your scripts here so we can see what you're doing wrong
Ok, well ok using this code I can access the other script: void Start () { GameObject femaleNude = GameObject.Find("femalenude"); FemaleNude femalenudeScript = femaleNude.GetComponent(); femalenudeScript.ChangeEnabled(false); }
But, when using the code later, in another function I get femalenudeScript is unknown error: femalenudeScript.ChangeEnabled(true);