Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by Cubemaster · Jan 19, 2017 at 11:02 PM · scripting problemvector3getcomponentturning

Get Vector3 From Another Script

I'm trying to make the player character turn, based on the way that it moves in another GameObject (The movement is controlled by a parent object and the turning is controlled by a child object). Right now my code looks like:

 using UnityEngine;
 using System.Collections;
 
 public class turn : MonoBehaviour {
 
 public float turnSpeed = 6.0F;
 
 
 private int moving = 0;    
 public Transform camera;
 public GameObject player;
 private Vector3 cameraDirection = Vector3.zero;
 private float movex;
 private float movey;
 private Vector3 turning;
 public Vector3 


     void FixedUpdate () {
         if (Input.GetAxis("Horizontal")==0F && Input.GetAxis("Vertical")==0F)
              moving = 0;
         else
              moving = 1;
         if (moving == 0)
             cameraDirection = camera.transform.forward;
         
     float turn = turnSpeed * Time.deltaTime;
     var script = player.GetComponent("Movement");
     Vector3 turnDirection = script.GetComponent("moveDirection") as Vector3;
     turning = Vector3.RotateTowards(transform.forward, turnDirection, turn, 0.0F);
     transform.rotation = Quaternion.LookRotation(turning);
     
     }
      
     }
 

and it gives me the error: "The 'as' operator cannot be used with a non-nullable value type 'UnityEngine.Vector3'". How can I fix this or get the Vector3 another way?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Landern · Jan 20, 2017 at 02:17 PM

As is a wonder type cast in that if it can no resolve then you can check for null, this means it can not be a value type(a struct, like a Vector3 is) unless you use Nullable, but that is a waste in this case. The problem you may have is if it can't find it it will throw with a general type cast, but you can change line 29 to:

 Vector3 turnDirection = (Vector3)script.GetComponent("moveDirection");

You may want to also use the generic version rather then that string version of GetComponent.

 Vector3 turnDirection = (Vector3)script.GetComponent<moveDirection>();

I also doubt that moveDirection is a Vector3 type and is really a script/class you have already written with a field or property with the Vector3 on it, if that is the case.

 moveDirection md = (Vector3)script.GetComponent<moveDirection>();
 Vector3 moveDirection = md.whateverContainsTheValueYouWantToGrab;
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

116 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 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 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 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 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 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 avatar image avatar image

Related Questions

Passing enemy statistic to FightController 0 Answers

NullReferenceException: Object reference not set to an instance of an object 1 Answer

How to call Animator from another script 0 Answers

creating 3d melee cone splash attack 1 Answer

Searching a function to get the rotation of one Vector3 in relation to another 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