Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 haldanemcfall · Dec 16, 2016 at 11:57 AM · getcomponentbool

Trouble getting a bool from player script for camera script

Hi,

My first question here - thanks in advance for your help.

I have a PlayerScript where sometimes the player (a cat character) is sitting, and so a bool in that script is then set to true.

I'd like to get this bool and use it in the script that is attached to my camera - so that the camera moves to LookAt a different spot (an empty child of the player that is ahead of the player) rather than looking at the player's transform which should happen when sitting is set to false. I've read some answers and material but feel I must be missing something as the camera script is not finding the sitting value from the PlayerScript. Any help is appreciated.

Again - the PlayerScript contains the bool "sitting" and this works fine within that script .

Here is the separate script attached to the camera:

 using System;
 using UnityEngine;
 
 namespace UnityStandardAssets.Cameras
 
 {
 
     public class LookatTarget : MonoBehaviour    {
 
         private Transform playerTransform;
         public GameObject catPlayer;
         private PlayerScript playerScript;
 
         void Start () {
 
             playerScript = catPlayer.GetComponent<PlayerScript>();
 
         }
 
         void Update () {
             if (playerScript.sitting == true) {
                 playerTransform = GameObject.Find("sitSpot").transform;
             } else {
                 playerTransform = GameObject.FindWithTag ("Player").transform;
             }
             transform.LookAt (playerTransform);
         }
     }
 }
 

And here is the error:

Assets/Cameras/Scripts/LookatTarget.cs(18,25): error CS0246: The type or namespace name `PlayerScript' could not be found. Are you missing a using directive or an assembly reference?

EDIT: Here is the PlayerScript. I stripped out some collision stuff that I think is irrelevant to my problem here in order to try to make it easier to follow.

 using UnityEngine;
 using System.Collections;
 using UnityStandardAssets.CrossPlatformInput;
 
 [RequireComponent (typeof (Rigidbody))]
 [RequireComponent (typeof (CapsuleCollider))]
 
 public class PlayerScript : MonoBehaviour {
 
     private Animator anim;
     private int animpar; 
     public float moveSpeed = 6.0f;
     private Vector3 moveDirection = Vector3.zero; // Direction we're moving in.
     private Vector3 targetDirection = Vector3.zero; // Direction we want to move in.
     private float rotateSpeed = 5; // Speed to rotate at.
 
     private Vector3 right = Vector3.zero;
     private Vector3 forward = Vector3.zero;
     public Vector3 MoveVector{ set; get;}
 
     public Vector3 cameraLevelVec;
     public Vector3 camActualRot;
 
     public bool canJump = true;
     public bool sitting = false;
     private bool touching = false;
     private bool nottouching = true;
 
     public float gravity = 20.0f;
     public float jumpHeight = 2.0f;
     private bool grounded = false;
 
     private float camAngleZ;
     private float smoothing;
 
     public Rigidbody rb;
        public Camera MainCamera;
 
     private Transform lawnCameraSpotTransform;
     private Transform pressCameraSpotTransform;
 
     private Transform camSitSpotTransform;
     private Transform catFollowCamSpotTransform;
 
     private Transform cameraActualTransform;
 
     //active camera
     private Transform cameraTransform;
  
     public bool freezeRotation;
 
     float horizontal = 0.0f;
     float vertical = 0.0f;
     bool directionChosen = false;
     bool newRoomTriggered = false;
 
     private Vector2 touchOrigin = -Vector2.one;
     private Vector2 touchDirection;
 
     public BoxCollider lawnCollider;
     public BoxCollider pressCollider;
 
     private bool lawnCollided = true;
     private bool pressCollided = false;
     
     // Use this for initialization
     void Start () {
 
         rb = GetComponent<Rigidbody>();
         anim = gameObject.GetComponentInChildren<Animator> ();
 
         MainCamera = GameObject.Find("MainCamera").GetComponent<Camera>();
   
         lawnCameraSpotTransform = GameObject.Find("lawnCameraSpot").transform;
         pressCameraSpotTransform = GameObject.Find("pressCameraSpot").transform;
 
         camSitSpotTransform = GameObject.Find("camSitSpot").transform;
         cameraActualTransform = GameObject.Find("MainCamera").transform;
 
         //camera starting spot for movement/direction
         cameraTransform = lawnCameraSpotTransform;
 
         //move camera itself to start spot
         Vector3 cameraPos = cameraTransform.position;
         MainCamera.transform.position = cameraPos;
 
         GetComponent<Rigidbody>().freezeRotation = true;
         GetComponent<Rigidbody>().useGravity = false;
     }
 
     // Update is called once per frame
     void Update () {
 
         catFollowCamSpotTransform = GameObject.Find("catFollowCamSpot").transform;
 
         if (Input.touchCount > 0) {
             Touch touch = Input.touches [0];

                 if (touch.phase == TouchPhase.Began) {    
 
                 touchOrigin = touch.position;
 
             // Determine direction by comparing the current touch position with the initial one.
             } else if (touch.phase == TouchPhase.Moved) {
     
                 touchDirection = touch.position - touchOrigin;
                 horizontal = touch.position.x - touchOrigin.x;
                 vertical = touch.position.y - touchOrigin.y;
                 touching = true;
             
             } else if (touch.phase == TouchPhase.Ended) { 
                     
                     touching = false;
 
                     horizontal = 0.0f;
                     vertical = 0.0f;
                     
                     touchOrigin.x = -1;
                     touchOrigin.y = -1;
             }
         }
                 
         MoveVector =  CameraFixed();
 
         Move ();
 
         SwitchCameraPosition ();
 
         //Animation
         if (grounded) {
 
             //if (CrossPlatformInputManager.GetAxis ("Vertical") != 0) {
             if (vertical != 0) {
                 anim.SetInteger ("AnimPar", 1);
                 sitting = false;
                 canJump = true;
                 smoothing = 0.1f;
 
             } else {
                 anim.SetInteger ("AnimPar", 0);
                 //canJump = false;
             }

             //Sit
             //if (CrossPlatformInputManager.GetButton ("Sit")) {
             if (Input.touchCount == 3) {
                 
                 anim.SetInteger ("AnimPar", 2);
                 sitting = true;
                 cameraTransform = camSitSpotTransform;
                 smoothing = 0.2f;
             }
 
             // Jump
             //if (canJump && CrossPlatformInputManager.GetButton ("Jump")) {
             if (canJump && (Input.touchCount == 2)) {
                 //&& (sitting == false)
                 
                     anim.SetInteger ("AnimPar", 4);
                     rb.AddForce (Vector3.up * jumpHeight, ForceMode.Impulse);
             }

         // We apply gravity manually for more tuning control
         rb.AddForce(new Vector3 (0, -gravity * rb.mass, 0));
 
         grounded = false;
     }
 
     private Vector3 CameraFixed() {
 
         Vector3 dir = Vector3.zero;
         dir.x = horizontal;
         dir.z = vertical;
 
         //get the forward-facing direction of the camera
         Vector3 cameraForward = cameraActualTransform.TransformDirection (Vector3.forward);
         cameraForward.y = 0;    //set to 0 because of camera rotation on the X axis
 
         //get the right-facing direction of the camera
         Vector3 cameraRight = cameraActualTransform.TransformDirection (Vector3.right);
 
         if (dir.magnitude > 1)
             dir.Normalize ();
         
         Vector3 refDir = dir.x * cameraRight + dir.z * cameraForward;
     
         //return refDir.normalized * MoveVector.magnitude;
         return refDir;
 
     }
 
 
     private void Move() {
 
         if (sitting == false) {
             rb.MovePosition (rb.position + MoveVector * moveSpeed * Time.deltaTime);
 
             //rotate cat in direction of movement
             if ((horizontal != 0) && (vertical != 0)) {
                 
                 transform.rotation = Quaternion.LookRotation(MoveVector);
             }
         }
     }
 
     void OnCollisionStay (Collision other) {
 
         if (other.gameObject.tag == "Ground") {
             grounded = true;    
         }   
     }
 
 
     public void SwitchCameraPosition () {
 
         //if (touching == false) {
         if (sitting == false) {
             
             if (pressCollided == true) {
                 cameraTransform = pressCameraSpotTransform;
             }
         }
 
 
         MainCamera.transform.position = Vector3.Lerp (cameraActualTransform.position, cameraTransform.position, smoothing);
 
     }
 }
 



Thanks!!

Comment
Add comment · Show 2
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 Pengocat · Dec 16, 2016 at 12:46 PM 0
Share

Did you add PlayerScript to another namespace in the PlayerScript file and forgot to include it? Hard to tell what is wrong without looking at the playerscript.

avatar image haldanemcfall Pengocat · Dec 16, 2016 at 05:32 PM 0
Share

Hi Pengocat - I've added that now here as well! Thanks!

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by kumade · Dec 16, 2016 at 03:06 PM

@haldanemcfall, I'm not a pro in any way in C# namespaces, but logic tells me that: You need to try to make PlayerScript visible for LookatTarget. You may either

  • remove namespace UnityStandardAssets.Cameras from LookatTarget file, or

  • remove some namespace definition from PlayerScript file. So they both will be in "default" namespace.

  • Or use 'using' instruction for PlayerScript namespace at the top of LookatTarget file the same way You do with System and UnityEngine.

Comment
Add comment · Show 3 · 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 haldanemcfall · Dec 16, 2016 at 05:35 PM 0
Share

Hi kamde_, thanks for your help so far. I've added that script above.

I tried removing the namespace line from the LookatTarget script, but I'm still getting the same error. Would adding 'using PlayerScript;' to the beginning of the LookatTarget script be the right approach? At the moment that is also giving me a red word. Thanks again!

alt text

screen-shot-2016-12-16-at-123510-pm.png (120.0 kB)
avatar image kumade haldanemcfall · Dec 16, 2016 at 08:19 PM 0
Share

Seems ok to me. Could You also provide a screenshot with project structure, specifically the locations of the PlayerScript and LookatTarget, in which folders they are relatively to each other. If , for instance , they are in different projects (I mean, different $$anonymous$$onoDevelop or $$anonymous$$SVS projects of the same Unity project) then You may try something like this http://stackoverflow.com/questions/4286599/referenced-project-gets-lost-at-compile-time

avatar image haldanemcfall kumade · Dec 17, 2016 at 03:50 PM 0
Share

Thank you - the PlayerScript file itself was not in the same "Scripts" folder as the LookatTarget script - moving them both into the main scripts folder, and removing the namespace lines has done the trick! Thanks again!!

avatar image
1

Answer by Laiken · Dec 17, 2016 at 05:53 AM

If it's a single player game and there's only a PlayerScript in the scene, try

 void Start () {
  
              playerScript = FindObjectOfType(typeof(PlayerScript)) as PlayerScript;
  
          }

If there's only one PlayerScript in the scene, you can also make the sitting bool be static and make

 if (PlayerScript.sitting == true) // note the uppercase "P"

But before that, just in case, make sure that you dragged the GameObject catPlayer to the catPlayer slot in the camera script (on the inspector).

Also, again, just in case, make sure that you put the PlayerScript on the catPlayer directly (and not a child of catPlayer)

Comment
Add comment · Show 2 · 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 haldanemcfall · Dec 17, 2016 at 03:50 PM 0
Share

Thank you! I was getting an error because I had not connected them in the inspector.

avatar image Laiken · Dec 17, 2016 at 09:03 PM 0
Share

I'm glad I was of help.

I will appreciate If you up vote my answer so that I can gain 10 reputation. With that I will be able to post answers without having to wait for moderation approval.

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

7 People are following this question.

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

Related Questions

check if the bool is true from other script c# 1 Answer

How do I check a bool from another script?? 2 Answers

how to use bool from another script in if statment? 2 Answers

Get bool value from one script to another (C#) 2 Answers

Writing a bool to a separate script without scripts name? 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