Jump to content
Sign in to follow this  
nusun

C# for unity

Recommended Posts

Nesanāk saprast ko es daru nepareizi.

Kods

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class Animal : MonoBehaviour {

	public float radius;
	public float timer;

	private Transform target;
	private NavMeshAgent agent;
	private float currentTimer;

	private bool idle;
	public float idleTimer;
	private float currentIdleTimer;

	private Animation anim;

	void OnEnabled(){
		agent = GetComponent<NavMeshAgent> ();
		anim = GetComponent<Animation> ();
		currentTimer = timer;
		currentIdleTimer = idleTimer;
	}

	void Update(){
		currentTimer += Time.deltaTime;
		currentIdleTimer += Time.deltaTime;

	    if (currentIdleTimer >= idleTimer) {
			StartCoroutine ("switchIdle");
		}

		if (currentTimer >= timer && !idle) {
			Vector3 newPosition = RandomNavSphere(transform.position, radius, -1);
			agent.SetDestination(newPosition); // met error
			currentTimer = 0;
		}

		if (idle == true) {
			anim.CrossFade ("idle"); // met error
		} else {
			anim.CrossFade ("walk"); // met error
		}
	}

	IEnumerator switchIdle(){
		idle = true;
		yield return new WaitForSeconds (3);
		currentIdleTimer = 0;
		idle = false;
	}

	public static Vector3 RandomNavSphere(Vector3 origin, float distance, int layerMask){
		Vector3 randomDirection = Random.insideUnitSphere * distance;
		randomDirection += origin;

		NavMeshHit navHit;
		NavMesh.SamplePosition(randomDirection, out navHit, distance, layerMask);

		return navHit.position;
	}

}

Pie rindām galā pierakstīju kuras met error.

Šeit ir errors

NullReferenceException: Object reference not set to an instance of an object
Animal.Update () (at Assets/Scripts/Animal.cs:45)

Share this post


Link to post
Share on other sites

Random minējums - nav nostrādājis OnEnabled līdz ar to agent un anim nav inicializēti

Share this post


Link to post
Share on other sites

Tur jau viss pateikts pēc errora. Tev pašā unity pie skripta settingiem nav padots līdzi aģent'a objekts. T.i - drag&drop. Vai arī norādot, ka tur būs navMeshAgent esi iedevis iekšā citu objektu. Vismaz tā izskatās. Es vairāk uz pirmo variantu lieku. 

Edited by MārisL

Share this post


Link to post
Share on other sites
13 hours ago, MārisL said:

Tur jau viss pateikts pēc errora. Tev pašā unity pie skripta settingiem nav padots līdzi aģent'a objekts. T.i - drag&drop. Vai arī norādot, ka tur būs navMeshAgent esi iedevis iekšā citu objektu. Vismaz tā izskatās. Es vairāk uz pirmo variantu lieku. 

Ir padots viss. Man Loyc variants šķiet ticamāks, es tikai nevaru saprast kā viņu palaist. Ar c# uzsāku tik pirms dazām nedēļām. Nekur citur kā iekš unity neesmu viņu vēl lietojis, līdz ar to esmu diezgan liels nejēga.

Screen no mana projekta.

original.png

 

Tātad, paņēmu

	void OnEnabled(){
		agent = GetComponent<NavMeshAgent> ();
		anim = GetComponent<Animation> ();
		currentTimer = timer;
		currentIdleTimer = idleTimer;
	}
///////////Partaisīju par
          
	void Start(){
		agent = GetComponent<NavMeshAgent> ();
		anim = GetComponent<Animation> ();
	}

	void OnEnabled(){
		currentTimer = timer;
		currentIdleTimer = idleTimer;
	}

Tadāāāā viss strādā.

Edited by nusun
Found a solution

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.