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 /
avatar image
0
Question by rallen9 · Apr 24, 2015 at 05:25 PM · scripting problemnamespaceobject referencecasting

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);
     }
 }
Comment
Add comment · Show 4
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 rallen9 · Apr 24, 2015 at 05:33 AM 0
Share

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);
     }
 }
 

avatar image jmorhart · Apr 24, 2015 at 05:39 PM 0
Share

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.

avatar image rallen9 · Apr 25, 2015 at 07:04 AM 0
Share

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?

avatar image dingben · May 02, 2015 at 08:30 PM 0
Share

in the docs see Unity Special Folders... seems a duhhh... perhaps I am not understanding your post

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Casting a GraphView node using GetNodeByGuid() returns null 1 Answer

Trying to track the BoxColliders of 2 Different Sets of Instantiated GameObjects 0 Answers

The type or namespace name could not be found. Are you missing a using directive or an assembly reference? 4 Answers

Platform dependent flags required in Using/Namespace definition? 1 Answer

How to import project 1 to another project , with same name of script with unique namespaces and different root folder? 1 Answer


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