So what I'm trying to do is call a function from another script.
public class Manager
// Use this for initialization
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"
Answer by vcjr12
Jul 08, 2013 at 09:12 PM
I tried this and it worked thanks for your help.
GameObject player = GameObject.FindGameObjectWithTag("Player");
Ohhh man. Feeling a little silly that I had mixed up a type I defined in my script rather than in scene editor (prefab.) You're awesome.
Answer by robertbu
Jul 08, 2013 at 08:50 PM
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:
Accessing Other GameObjects
Then you will use a variable pointing to that instance of the script to make the access. Something like:
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
Player.SetPlayerPosition playerStart = new Player.SetPlayerPosition();
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().
fixed the link
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a user guide.
If you are a new user, check out our FAQ for more information.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
For troubleshooting common problems with Unity 5.x Editor (including Win 10).
Answers and Comments
20 People are following this question.
Instantiating gameObject with custom Class properties
How exactly do Monobehaviour functions work?
Boolean from another script reads false even if i change it to true.
Making a Trait Mechanic?
Instantiating objects from a class? (C#)