access another script from FirstPersonController - c#

sorry for my English!
I have scripts called "MYJoystick.cs":
public class MYJoystick : MonoBehaviour
{
public static string myX;
}
and I want to access "myX", from "FirstPersonController.cs" in unity 5. I want to write this in the Update in the FirstPersonController Like this:
private void Update()
{
MYJoystick.myX += 1;
}
but it says "The name `MYJoystick' does not exist in the current context".
How can I fix this? should i add Using In FirstPersonController? using what? what is the default namespace in unity? I also tried to add Namespace for my script, but it didn't work too!

I just had this problem.
http://docs.unity3d.com/Manual/ScriptCompileOrderFolders.html
The basic rule is that anything that will be compiled in a phase after the current one cannot be referenced.
The phases of compilation are as follows:
Phase 1: Runtime scripts in folders called Standard Assets, Pro Standard Assets and Plugins.
Phase 2: Editor scripts in folders called Editor that are anywhere inside top-level folders called Standard Assets, Pro Standard Assets and Plugins.
Phase 3: All other scripts that are not inside a folder called Editor.
Phase 4: All remaining scripts (ie, the ones that are inside a folder called Editor).
Your FPSController c# script is in the standard assets folder, this is compiled before your other custom c# scripts that are in another folder.
To fix this you could rename the 'Standard Assets' to another name, or put this folder into another folder or take the FPSController script and put this with your other scripts.
A better solution would be to copy the FPSController standard assets script and put this into your custom folder and then modify this script instead.
I tried to change the compilation order of the scripts as a better solution, but it didn't seem to work. That's only for run time with Start() and Awake() I think between scripts.

Related

How to run a function inside Vuforia's DefaultTrackableEventHandler.cs

I'm trying to run a function inside DefaultTrackableEventHandler.cs after Vuforia detects the target by using the protected virtual void OnTrackingFound() but Vuforia can't detect the namespace that I'm using which is saved inside the Assets folder plus whenever I restart Visual Studio, every code that I've added to the DefaultTrackableEventHandler is deleted and the file is reset back to its original state. I think the problem might lie in the fact that DefaultTrackableEventHandler.cs is located inside the Packages folder while the script that I'm using for the namespace is inside the Assets folder.
How do I resolve this issue?

Can I run BuildPipeline.BuildAssetBundles in playmode?

Hi everyone I need a help with Unity. I need to run the method 'BuildPipeline.BuildAssetBundles' after I click a button while i'm using the built software, is it possible to do? I know that this is an editor script, but is there a way to call it in a 'non-editor script?
Thanks at all
No classes that are part of the UnityEditor namespace will work in a build. Unity will simply refuse to try to do it. You can use things from AssetBundle because it's a part of UnityEngine, but you can't use BuildPipeline in a build. Unity allows the UnityEditor namespace to be include only in files that are marked as being editor files, usually by being placed in folders called "Editor" and those files are excluded from the build.
You can use BuildPipeline in playmode inside of Unity Editor's player though, but that's about it.

Cannot load AssetBundle because "it is not compatible with the newer version of the Unity runtime"

I am trying to load an AssetBundle from a file, however I get the following error:
The AssetBundle 'path\to\file' could not be loaded because it is not compatible with this newer version of the Unity runtime. Rebuild the AssetBundle to fix this error.
I build my AssetBundle as shown on the Unity wiki:
using UnityEditor;
namespace Editor
{
public class CreateAssetBundles
{
[MenuItem("Assets/Build AssetBundles")]
private static void BuildAllAssetBundles()
{
BuildPipeline.BuildAssetBundles("Assets/AssetBundles",
BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
}
}
}
This generates a correct looking AssetBundle, the manifest file also looks fine.
I load the AssetBundle with the following code:
var assetBundle = AssetBundle.LoadFromFile(path);
Both the AssetBundle and the game are built with the same version of Unity, version 2017.3.1f1 (64 bit). I've also tried building both with the latest available beta build, but this did not resolve the issue.
Changing the BuildTarget to BuildTarget.StandaloneWindows64 also does not resolve the issue.
The Unity docs are outdated a bit on AssetBundles, since Unity 2017 they introduced an entire new assetbundle system, which is easier to use and works with an improved UI called the AssetBundle Browser
I've had issues myself when switching from Unity 5.x to 2017.x using assetbundles, and it actually required me to use the new assetbundle system, and build/load through that to get them to work again.
Get the Assetbundle browser:
Download the AssetBundle Browser from Unity's GitHub
Add the downloaded files to your Unity project
Go to Window
AssetBundle browser
Building an assetbundle:
here you will see two tabs, "configure" and "Build". Select the assetbundle you want to build by dragging a prefab of the object into the configure tab. you'll get a question asking if you want to build it as one big bundle or multiple seperate bundles, select whichever you prefer.
The Browser will also give a warning if multiple bundles share the same assets, and propose to make a single seperate bundle containing all shared resource, depending on how many and how big your bundles are this can save quite alot of space.
Then if you go to the "Build tab" you can select for which platform you want to build and the output path, along with some additional options such as compression type. Then all you have to do is click "Build" to build your new assetbundle compatible with unity 2017.x
Loading an Assetbundle:
Loading an assetbundle from a file is as simple as using the following piece of code: AssetBundle myAssetBundle = AssetBundle.LoadFromFile(path);
You can also load assetbundles from Memory (taking in bytes) or load directly from a stream.
An additional bonus to the new AssetBundle browser is that you can customize it however you need All files can be found in /Assets/Editor/AssetBundleBrowser/. for example I included the functionality to automatically upload all bundles to an FTP after its done building.
Edit: The Unity AssetBundle browser tool works for version 5.6 or higher.

Load serverside c# scripts and then attach a scripts to game object

I have Two question.
1. How can I load existing c# scripts from external path to my apk file.
2. How can I attach scripts in a game object.
Assuming that I create a sphere and then build an apk file. when i run my unity app, you can see the sphere,which is not moving. After building apk file, i'd like to move the sphere .
Scripts are compiled objects, so I believe you can't load them in dynamically. In addition, I believe Android wouldn't allow it anyway, as loading scripts in dynamically is considered a security risk. You could download an app, which downloads new virus-type scripts that weren't in the original code...
you can attach C# script to game object and disable it(unchecked), and after build apk file you can enable it by a button in run time,

How to use public variables in Unity scripts put into asset bundles

I've searched on this question and found a lot of info related, but some inconsistencies, and no clear answer.
In a Unity project, I'm creating asset bundles with gameobjects that have scripts (C#). I've learned how to do that, and it works.
My problem is, I ultimately need to preserve the values of the public variables in the scripts that were assigned before building the asset bundle. That is, my prefab has a script with public variables. I instance it many times with different public values for each instance. I need all that to survive the trip through the asset bundle.
My current process is thus:
Remove scripts from their game objects.
Build the scripts into assemblies
Pack game objects and assemblies into asset bundles
Download the bundles to new project
Unpack the asset bundles and Instantiate the game objects
Attach script assemblies to game objects with AddComponent()
The issue is step 1. I remove the scripts from the game objects before bundling because if I don't, it complains when unpacking that the referenced script is missing. And also, since step 6 uses AddComponent to attach it, I cant really do that if that script is already an attached component. So removing them before bundling solves those problems.
What am I missing? Where is my method at fault? I read that the public values are saved in the script reference on the game object when bundled, but of course I lose that when I remove the script components.
So, please, how do I maintain public values thru the asset bundling process? Should I NOT be removing script components before bundling? If so, how should I be managing the scripts differently? Would I need to bundle both the script assemblies AND source files? (I currently only bundle the assemblies), and wouldn't that create a redundancy when attaching the assembly?
Unity does not store assemblies in bundles. It stores only serialization data for scripts (that's your public variables, and variables with [SerializeField] attribute). But it also stores the hash of the script assembly for every script type, so you have to build bundles in the same project with the main executable to avoid missing references.
So, you don't have to remove scripts from their GameObjects, but you have to build bundles in the same unity project and rebuild them, when code changes.

Categories

Resources