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 /
This question was closed May 13, 2014 at 12:34 PM by Cerbion_ for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Cerbion_ · Nov 05, 2013 at 01:14 PM · c#objectmonodevelop

Setting Player Instance for Scripts doesn't work.

Hello there, I am currently sitting on a really crappy problem, below you see my code where the player gets spawned into the world (using the PUN Plugin) and that works fine, the camera gets attached as well and follows the player accordingly, but I just don't get the InputScript to work.

 void OnJoinedRoom()
     {
         player = PhotonNetwork.Instantiate("Player", Vector3.zero, Quaternion.identity, 0);    //Instantiate the Player Clone
         Camera.main.GetComponent<CameraScript>().target = player.transform;                    //Let the Camera Target the player
         InputScript.fetch_movement = true;    // Start fetching input to move character
         InputScript.target = player;        // Give InputScript the player gameobject
     }

It gives ingame the error that: "Object Reference is no set to an Instance of an Object".. and I tried various things but I can't get it to work how I want it...

Here's my InputScript: public class InputScript : MonoBehaviour { public static bool fetch_movement;

     public static GameObject target;
     
     void Update ()
     {
         if(fetch_movement)
         {
             
             if(Input.GetKeyDown(KeyCode.A))
             {
                 target.GetComponent<PlayerMovement>().moving_left = true;
             }
             if(Input.GetKeyUp (KeyCode.A))
             {
                 target.GetComponent<PlayerMovement>().moving_left = false;
             }
             if(Input.GetKeyDown(KeyCode.D))
             {
                 target.GetComponent<PlayerMovement>().moving_right = true; 
             }
             if(Input.GetKeyUp (KeyCode.D))
             {
                 target.GetComponent<PlayerMovement>().moving_right = false;
             }
             if(Input.GetKeyDown(KeyCode.W))
             {
                 target.GetComponent<PlayerMovement>().moving_up = true;
             }
             if(Input.GetKeyUp (KeyCode.W))
             {
                 target.GetComponent<PlayerMovement>().moving_up = false;
             }
             if(Input.GetKeyDown(KeyCode.S))
             {
                 target.GetComponent<PlayerMovement>().moving_down = true;
             }
             if(Input.GetKeyUp (KeyCode.S))
             {
                 target.GetComponent<PlayerMovement>().moving_down = false;
             }
         }
     }
 }

Hint: The game is 2D, in case you were wondering why W is used for UP and S for DOWN.

I appreciate any kind of help/ideas!

Comment
Add comment
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

4 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by Tomer-Barkan · Nov 05, 2013 at 03:20 PM

Your target is probably null before the player is spawned. In Update(), add:

 if (target == null) 
     return;

In general it is good practice to always check that an object is not null, specially when you know it is at some point.

If this doesn't help, please share the exact line which throws the null pointer exception.

Comment
Add comment · Show 1 · 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
avatar image Cerbion_ · May 13, 2014 at 12:34 PM 0
Share

I'll accept this as the correct answer, although I have to add that I the "player" from the InputScript was a static value and therefore causing the problem, since it was null.

avatar image
0

Answer by ahaykal · Nov 05, 2013 at 01:21 PM

the problem is with this:

public static GameObject target; you need to initialize this value... why do you have it static? target = GameObject.Find("whatever_It_is_Called"); ..

Comment
Add comment · Show 1 · 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
avatar image Cerbion_ · Nov 05, 2013 at 02:32 PM 0
Share

But wouldn`t that be problematic, since the player prefabs are all clones? So if a second player (or more) connects, he would basically take over ALL the player movements? :I And I made it static, because I tested things out, but even if the variable is non static, the player gameobject doesn't get passed onto the InputScript for some reason...

avatar image
0

Answer by RyanZimmerman87 · Nov 05, 2013 at 03:12 PM

Kinda hard to answer without knowing how the PhotonNetwork functions but perhaps that isn't really relevant.

Normally if I instantiate new objects I use a clone. For example:

 //prefab
 public GameObject playerPrefab;
 //instantiate object
 GameObject newPlayerObject;
 
 newPlayerObject = Instantiate(playerPrefab, , Vector3.zero, Quaternion.identity, 0);
 

That might help you a little bit since you mentioned concern about duplicate objects, you should never need a static variable for this kind of thing unless I'm missing something with your project.

However with your initial error message that might be caused by not having as GameObject at the end of your instantiate? I would think you wouldn't be able to compile if that were the case but you might wanna check that out. For example:

 newPlayerObject = Instantiate(playerPrefab, Vector3.zero, Quaternion.identity, 0) as GameObject;

Maybe the as Gameobject is only necessary if you use the clone method not sure exactly.

Comment
Add comment · Show 1 · 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
avatar image RyanZimmerman87 · Nov 05, 2013 at 03:16 PM 0
Share

Also noticed you use 4 entries for your instantiate is that something unique for the PUN network you are using? Assu$$anonymous$$g the 0 is time?

Perhaps the PUN network is causing this somehow but it's most likely something simple, Unity is kind of finicky sometimes with missing object references when setting up new systems.

avatar image
0

Answer by drod7425 · Nov 05, 2013 at 03:35 PM

This seems unnecessary. Why not just attach the InputScript to the character prefab? Set the prefab as the target in the inspector. Then once the player is instantiated, there's no turning things on, it just starts working.

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

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

panel hide and show in NGUI 1 Answer

JS to C# List type problem 1 Answer

What is frame, How OnGuI is called every frame? 2 Answers

Inspector vs Script: Component best practice? 1 Answer

How to pass variables from one object in one scene to another object in another scene? 3 Answers


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