x


Before updating to Windows 10 you may need to return your Pro license. More information here

The name '' does not exist in the current context

So what I'm trying to do is call a function from another script.

 public class Manager  
 {
          // Use this for initialization
      void Start()
         {
         SetPlayerPosition();
     }
 }
 


okay that's the Manager Sample , here the other

 public class Player : MonoBehaviour 
 {
     
    
     // Use this to set Player Position
      public void SetPlayerPosition()
      {
       transform.position = new Vector3(150, 0, 5);
      }
 
 }

I know this might be a simple question but this is bugging me since Ive done it before, I might even answer this myself after further research but just to post in case someone has same problem in future.

Note: The error is on the Manager.cs "The name 'SetPlayerPosition' does not exist in the current context"

more ▼

asked Jul 08, 2013 at 08:37 PM

avatar image

vcjr12
111 5 7 8

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

I tried this and it worked thanks for your help.

  void Start()
 {
         GameObject player = GameObject.FindGameObjectWithTag("Player");
         player.GetComponent<Player>().SetPlayerPosition();
 }
 
more ▼

answered Jul 08, 2013 at 09:12 PM

avatar image

vcjr12
111 5 7 8

(comments are locked)
10|3000 characters needed characters left

You need to create a reference to a specific instance of the Player script (i.e. to the game object it is attached to). The multiple ways to do this are covered here:

http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

Then you will use a variable pointing to that instance of the script to make the access. Something like:

playerScript.SetPlayerPosition();

more ▼

answered Jul 08, 2013 at 08:50 PM

avatar image

robertbu
115k 27 79 260

i tried this Player.SetPlayerPosition playerStart = new Player.SetPlayerPosition(); but then I get Is a method bu tis being used as a type. But thanks Ill take a look

Jul 08, 2013 at 09:00 PM vcjr12

Read the link. You don't use new() with Monobehaviour. You need to either drag and drop in the Inspector, or you can use GameObject.Find() and GetComponent().

Jul 08, 2013 at 09:11 PM robertbu
(comments are locked)
10|3000 characters needed characters left

All you need to do is change your name of the script to the name called in the script : http://c2n.me/38sn2HL Boom, done. P.S FOR THE LOVE OF GOD PLEASE SHARE THIS INFO

more ▼

answered Dec 10, 2014 at 02:19 AM

avatar image

sirshelley
11 2

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x721
x289
x145

asked: Jul 08, 2013 at 08:37 PM

Seen: 6378 times

Last Updated: Dec 10, 2014 at 02:19 AM