- Home /
Does namespace relate to a location on disk or in the project
I'm an experienced developer but new to Unity. I'm using Unity 5.0.1 and working with the CharacterThirdPersonAI scene from the sample pack. I added a button to the scene to instantiate multiple GameObjects instead of just the one from the sample pack. The existing logic uses a PreFab named TargetPicker which has an instance of the GameObject that will be moving assigned by the inspector. TargetPicker also has script PlaceTargetWithMouse which get the mouse click position and sets the GameObject transform.
I added a setter function to PlaceTargetWithMouse to set the GameObject script. I added a button to the UI which has my script AddPlayerToField, a reference TargetPicker assigned by the inspector from the Hierarchy. My intention is to instantiate the player and pass it to the setter function in PlaceTargetWithMouse. I have tried several different ways but cannot get a reference to PlaceTargetWithMouse to call the setter function. PlaceTargetWithMouse has the namespace setting "namespace UnityStandardAssets.SceneUtils". If I try to reference SceneUtils in my script, the compiler complains that it does not exist in UnityStandardAssets. I tried to comment out the namespace setting in PlaceTargetWithMouse but it did not make a difference. I've spent quite a bit of time and don't really know where to go from here. Any help is appreciated. I commented out my attempts below to get the compiler to stop complaining.
using UnityEngine;
using System.Collections;
public class AddPlayerToField : MonoBehaviour {
private int numPlayers = 0;
private GameObject go;
// private SceneUtils.PlaceTargetWithMouse mtp;
public GameObject targetLocator;
public void Start() {
go = (GameObject)Instantiate (targetLocator);
//mtp = go.GetComponent<PlaceTargetWithMouse>();
}
public void AddGamePlayer(GameObject player) {
numPlayers += 1;
GameObject player1 = (GameObject)Instantiate(player, new Vector3(numPlayers * 2.0F, 0, 0), Quaternion.identity);
//((PlaceTargetWithMouse)targetLocator.GetComponent(typeof(PlaceTargetWithMouse))).SetTargetPlayer(player1);
//mtp.SetTargetPlayer(player1);
}
}
I added a debug loop to show the components of targetLocator and it shows what the inspector shows 4 components. 1 transform, 2 platform specific scripts and PlaceTargetWith$$anonymous$$ouse. Im showing the PlaceTargetWith$$anonymous$$ouse debug line and my current version of the script.
With this version, the compiler says that SceneUtils does not exist in the namespace UnityStandardAssets. If I comment out the using statement on line 3, it says type PlaceTargetWith$$anonymous$$ouse cannot be found. How do I get to the function SetTargetPlayer in script PlaceTargetWith$$anonymous$$ouse?
Debug showing PlaceTargetWith$$anonymous$$ouse TargetPicker (UnityStandardAssets.SceneUtils.PlaceTargetWith$$anonymous$$ouse) UnityEngine.Debug:Log(Object) AddPlayerToField:Start() (at Assets/Standard Assets/Characters/ThirdPersonCharacter/Scripts/AddPlayerToField.cs:19)
Current version of code using UnityEngine; using System.Collections; using UnityStandardAssets.SceneUtils;
public class AddPlayerToField : $$anonymous$$onoBehaviour {
private int numPlayers = 0;
private PlaceTargetWith$$anonymous$$ouse ptwm;
public GameObject targetLocator;
public void Start() {
ptwm = targetLocator.GetComponent<PlaceTargetWith$$anonymous$$ouse>();
Debug.Log(targetLocator.ToString());
Component[] allComponents = targetLocator.GetComponents<Component>();
Debug.Log("Number of Components: " + allComponents.Length.ToString());
for (int x=0; x< allComponents.Length; x++) {
Component com = allComponents[x];
Debug.Log (com.ToString());
}
}
public void AddGamePlayer(GameObject player) {
numPlayers += 1;
GameObject player1 = (GameObject)Instantiate(player, new Vector3(numPlayers * 2.0F, 0, 0), Quaternion.identity);
ptwm.SetTargetPlayer(player1);
}
}
I'm not all that familiar with Unity's standard asset packages, but I'm guessing you need
using UnityStandardAssets;
or maybe
using UnityStandardAssets.SceneUtils;
at the top of your script.
As I mentioned earlier, I started working with the Standard Assets. I originally had my script in a subfolder off the Standard Assets root folder and was not able to find SceneUtils. I found that if I moved my script from the Standard Assets root folder to the Sample Scenes root folder in the Project window, I can add the using UnityStandardAssets.SceneUtils
to my script and was able to get the reference.
Can someone please help me understand why this is so or point me to some the documentation that explains this?
in the docs see Unity Special Folders... seems a duhhh... perhaps I am not understanding your post
Answer by tanoshimi · Apr 25, 2015 at 07:14 AM
Namespaces are nothing to do with physical location of files on disk or hierarchy in project. They are defined purely in scripts. However, what does matter is the compilation order of your scripts, as the class that defines a namespace must be compiled before any class that attempts to reference it.
The reason why your script is failing to find the StandardAssets namespace is because (from your last comment), you've placed your script in the /Standard Assets folder itself. Your script is getting compiled before that Standard Assets it is attempting to reference.
/Standard Assets is a special folder that Unity uses, the contents of which are compiled before other scripts. You should not generally speaking place any of your own scripts in this folder. Put them somewhere else in the project, and you can be sure that the Standard Assets will have been compiled and be available to reference.
This is explained further/better at http://docs.unity3d.com/Manual/ScriptCompileOrderFolders.html