There are several instances of the gameobject “Mover” in the scene.
When the “follower” game actor is over the “mover”, the mover should tell the “follower” which direction and speed to go.
I’ve tried as many different ways i could find of using GetComponent, but i am now getting a null reference exception, “Object reference not set to an instance of an object”. It sounds pretty self explanatory but I haven’t been able to find anything on it.
I need to get the variables for speed and angle from the Mover object detected with my raycasthit.
void Update () {
RaycastHit hit = new RaycastHit ();
// Let's see if there is something below us.
if (Physics.Raycast(transform.position, Vector3.forward, out hit , castRange)) {
Debug.Log ("I am on something...");
Debug.Log (hit.collider.gameObject.tag);
// Is something below us tagged as a Mover?
if (hit.collider.gameObject.tag == "Mover") {
Debug.Log ("Oh, I am on a Mover!");
moving = true;
}
// If I am on a mover, i want to move the direction and speed values given by that mover
if (moving = true) {
moveDirection = GetComponent<moverAttributes>().moverDirection; //(hit.collider.gameObject);
print (moveDirection);
Update:
Right now the current iteration of the code is at:
using UnityEngine;
using System.Collections;
public class followerAttributes : MonoBehaviour {
bool moving = false;
float oldMoveSpeed = 3.0f; //planning to remove
Vector3 East = Vector3.left; //planning to remove
float moveSpeed;
float castRange = 1000;
float moveDirection;
//Quaternion rotation = Quaternion.identity;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
RaycastHit hit = new RaycastHit ();
// Let's see if there is something below us.
if (Physics.Raycast(transform.position, Vector3.forward, out hit , castRange)) {
Debug.Log ("I am on something...");
Debug.Log (hit.collider.gameObject.tag);
// Is something below us tagged as a Mover?
moving = hit.collider.CompareTag("Mover");
// If I am on a mover, i want to move the direction and speed values given by that mover
if (moving) {
Debug.Log ("Oh, I am on a Mover!");
//This is the line giving me a null reference exception error
moveDirection = hit.collider.GetComponent<moverAttributes>().moverDirection;//(hit.collider.gameObject);
print (moveDirection);
float angleTest = moveDirection;
float xMove = Mathf.Sin (Mathf.Deg2Rad * angleTest);
float yMove = Mathf.Cos (Mathf.Deg2Rad * angleTest);
Vector3 thatWay = new Vector3 (yMove, xMove, 0);
transform.Translate (thatWay * oldMoveSpeed * Time.deltaTime);
print (xMove);
print (yMove);
}
}
}
}
I truncated all the stuff commented out…
The demo i have working is here: http://soylentworks.com/codesucks/Builds/TouchnDrag.html
I need the sphere’s to follow the angle of the belts. I am a total noob to coding, so i am probably overlooking something basic.