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 /
  • Help Room /
avatar image
0
Question by Reself · Jan 16, 2017 at 07:36 AM · instantiate

Why my object dont want to Update ?

My Script is loading a prefab into the scene the prefab is the same as actually using but the problem is that the object do nochting it don updat enothing i should die after seconds but it dont lose enegergy Here is my script pls help me :)

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

public class NeuronNetwork : MonoBehaviour {

 public int hiddenNeuronCount;
 public int inputNeuronCount;
 public int outputNeuronCount;

 public float cooldowntimer;

 public List<Neuron> inputNeuron = new List<Neuron>();
 public List<Neuron> hiddenNeuron = new List<Neuron>();
 public List<Neuron> outputNeuron = new List<Neuron>();

 public List<Connection> inputConnection = new List<Connection>();
 public List<Connection> outputConnection = new List<Connection>();

 public GameObject creaturePrefab;

 public float Energy;

 public GameObject GameManager;
 public GameObject mapManager;

 public Vector2 upgradePositionbody;
 public Vector2 upgradePositionfeeler;

 public bool canbirth = true;
 private GameObject child;

 void Start()
 {

     cooldowntimer = 30;

     //Set inputNeuronen
     AddNeuron("inputNeuron", "Bias", 5000);
     AddNeuron("inputNeuron", "FoodvaluePosition", 5);
     AddNeuron("inputNeuron", "FoodvalueFeeler", 5);
     //AddNeuron("inputNeuron", "FeelerOcclusion", 5);
     AddNeuron("inputNeuron", "Energy", 5);
     //AddNeuron("inputNeuron", "Age", 5);
     //AddNeuron("inputNeuron", "GeneticDifferenceFeeler", 5);
     //AddNeuron("inputNeuron", "WasAttacked", 5);
     AddNeuron("inputNeuron", "WateronFeeler", 5);
     AddNeuron("inputNeuron", "WateronCreature", 0);

     //Set outputNeuronen
     AddNeuron("outputNeuron", "Birth", 0);
     AddNeuron("outputNeuron", "rRotate", 0);
     AddNeuron("outputNeuron", "lRotate", 0);
     AddNeuron("outputNeuron", "Forward", 0);
     //AddNeuron("outputNeuron", "Feelerangle", 0);
     //AddNeuron("outputNeuron", "Attack", 0);
     AddNeuron("outputNeuron", "Eat", 0);

     //Set hiddenNeuron
     AddNeuron("hiddenNeuron", "h1", 0);
     AddNeuron("hiddenNeuron", "h2", 0);
     AddNeuron("hiddenNeuron", "h3", 0);
     AddNeuron("hiddenNeuron", "h4", 0);
     AddNeuron("hiddenNeuron", "h5", 0);
     AddNeuron("hiddenNeuron", "h6", 0);




     //Generate inputConnection
     hiddenNeuronCount = hiddenNeuron.Count;
     inputNeuronCount = inputNeuron.Count;
     outputNeuronCount = outputNeuron.Count;
     

     GenerateConnections();

     Energy = 150;

     GameManager = GameObject.FindGameObjectWithTag("manager");
     mapManager = GameObject.FindGameObjectWithTag("mmanager");
 }

 void Update()
 {
     if (Energy > 0)
     {
         CalculateNeuron();
         AssignOutputNeuron();


         upgradePositionbody = new Vector2((int)Math.Round(transform.GetChild(0).position.x), (int)Math.Round(transform.GetChild(0).position.z));
         upgradePositionfeeler = new Vector2((int)Math.Round(transform.GetChild(1).position.x), (int)Math.Round(transform.GetChild(1).position.z));
         //SetInputValue();

         if (cooldowntimer > 0)
         {
             cooldowntimer -= Time.deltaTime;
         }
         if (cooldowntimer < 0)
         {
             cooldowntimer = 0;
         }
         if (cooldowntimer == 0)
         {
             //Birth();

             canbirth = true;
             if (Energy >= 50 && canbirth == true)
             {
                 GameManager manager = GameManager.GetComponent<GameManager>();
                 child = Instantiate(creaturePrefab, transform.position - new Vector3(1, 0, 0), Quaternion.identity);
                 NeuronNetwork network = child.GetComponent<NeuronNetwork>();
                 network.cooldowntimer = 30;
                 network.inputConnection = inputConnection;
                 network.outputConnection = outputConnection;
                 for (int l = 0; l < network.inputConnection.Count; l++)
                 {
                     //FEHLER NULLREFERENZEXEPTION
                     network.inputConnection[l].weight += UnityEngine.Random.Range(-0.1f, 0.1f);
                 }
                 for (int p = 0; p < network.outputConnection.Count; p++)
                 {
                     network.outputConnection[p].weight += UnityEngine.Random.Range(-0.1f, 0.1f);
                 }
                 manager.birthes += 1;
                 manager.Creatures += 1;
                 //Energy -= 30;

             }
             cooldowntimer = 30;
             canbirth = false;
         }

         

         Energy -= Time.deltaTime * 5f;

         MapManager mapm = mapManager.GetComponent<MapManager>();

         /*for (int x = 0; x < mapm.worlddata.Count; x++)
         {
             if (upgradePositionbody == mapm.worlddata[x].blockpos)
             {
                 if (mapm.worlddata[x].id == 0)
                 {
                     Debug.Log("Grass body");
                     inputNeuron[1].value = 100;
                 }
                 if (mapm.worlddata[x].id == 2)
                 {
                     inputNeuron[5].value = 100;
                 }
             }
             else
             {
                 inputNeuron[1].value = 0;
                 inputNeuron[5].value = 0;
             }

             if (upgradePositionfeeler == mapm.worlddata[x].blockpos)
             {
                 if (mapm.worlddata[x].id == 0)
                 {
                     Debug.Log("Grass feeler");
                     inputNeuron[2].value = 100;
                 }
                 if (mapm.worlddata[x].id == 2)
                 {
                     inputNeuron[4].value = 100;
                 }
             }
             else
             {
                 inputNeuron[2].value = 0;
                 inputNeuron[4].value = 0;
             }
         }*/

         RaycastHit hitinfo;
         if (Physics.Raycast(transform.GetChild(0).transform.position - new Vector3(0, -0.2f, 0), -Vector3.up, out hitinfo))
         {
             if (hitinfo.transform.gameObject.tag == "Grass")
             {
                 Grass grassscript = hitinfo.transform.gameObject.GetComponent<Grass>();
                 inputNeuron[1].value = grassscript.foodlevel;
                 inputNeuron[5].value = 5;
             }
             else if (hitinfo.transform.gameObject.tag == "Water")
             {
                 inputNeuron[1].value = 5;
                 inputNeuron[5].value = 100;
             }
             //When no block is set value to 0
             else if (hitinfo.transform.gameObject == null)
             {
                 inputNeuron[1].value = 5;
             }
             else
             {
                 inputNeuron[1].value = 5;
                 inputNeuron[5].value = 5;
             }
         }
         if (Physics.Raycast(transform.GetChild(1).transform.position - new Vector3(0, -0.2f, 0), -Vector3.up, out hitinfo))
         {
             if (hitinfo.transform.gameObject.tag == "Grass")
             {
                 Grass grasscript = hitinfo.transform.gameObject.GetComponent<Grass>();
                 inputNeuron[2].value = grasscript.foodlevel;
                 inputNeuron[4].value = 5;
             }
             else if (hitinfo.transform.gameObject.tag == "Water")
             {
                 inputNeuron[2].value = 5;
                 inputNeuron[4].value = 100;
             }
             else
             {
                 inputNeuron[2].value = 5;
                 inputNeuron[4].value = 5;
             }
         }
         inputNeuron[3].value = Energy;
     }

     Death();
 }

 public void AddNeuron(string layer, string name, float value)
 {
     Neuron addneuron = new Neuron();
     addneuron.name = name;
     addneuron.value = value;
     if (layer == "inputNeuron")
     {
         inputNeuron.Add(addneuron);
     }
     if (layer == "hiddenNeuron")
     {
         hiddenNeuron.Add(addneuron);
     }
     if (layer == "outputNeuron")
     {
         outputNeuron.Add(addneuron);
     }
 }

 public void AddConection(string layer, float weight)
 {
     Connection addconnection = new Connection();
     addconnection.weight = weight;
     if (layer == "inputConnection")
     {
         inputConnection.Add(addconnection);
     }
     if (layer == "outputConnection")
     {
         outputConnection.Add(addconnection);
     }
 }

 public void SetInputValue()
 {
     MapManager mapm = mapManager.GetComponent<MapManager>();

     for (int x = 0; x < mapm.worlddata.Count; x++)
     {
         if (upgradePositionbody == mapm.worlddata[x].blockpos)
         {
             if (mapm.worlddata[x].id == 0)
             {
                 Debug.Log("Grass body");
                 inputNeuron[1].value = 100;
             }
             if (mapm.worlddata[x].id == 2)
             {
                 inputNeuron[5].value = 100;
             }
         }
         else
         {
             inputNeuron[1].value = 0;
             inputNeuron[5].value = 0;
         }

         if (upgradePositionfeeler == mapm.worlddata[x].blockpos)
         {
             if (mapm.worlddata[x].id == 0)
             {
                 Debug.Log("Grass feeler");
                 inputNeuron[2].value = 100;
             }
             if (mapm.worlddata[x].id == 2)
             {
                 inputNeuron[4].value = 100;
             }
         }
         else
         {
             inputNeuron[2].value = 0;
             inputNeuron[4].value = 0;
         }
     }
 }

 public void CalculateNeuron()
 {
     for (int x = 0; x < inputNeuronCount; x++)
     {
         for (int y = 0; y < hiddenNeuronCount; y++)
         {
             //FEHLER NULLREFERENZEXEPTION
             hiddenNeuron[y].value = inputNeuron[x].value * inputConnection[y].weight;
         }
     }

     for (int x = 0; x < hiddenNeuronCount; x++)
     {
         for (int y = 0; y < outputNeuronCount; y++)
         {
             outputNeuron[y].value = hiddenNeuron[x].value * outputConnection[y].weight;
         }
     }
 }

 public void GenerateConnections()
 {
     for (int x = 0; x < inputNeuronCount * hiddenNeuronCount; x++)
     {
         AddConection("inputConnection", UnityEngine.Random.Range(-0.5f, 0.5f));
     }


     for (int x = 0; x < hiddenNeuronCount * outputNeuronCount; x++)
     {
         AddConection("outputConnection", UnityEngine.Random.Range(-0.5f, 0.5f));
     }
 }

 public void AssignOutputNeuron()
 {
     RaycastHit hit;
     if (Physics.Raycast(this.transform.GetChild(0).transform.position - new Vector3(0, -0.2f, 0), -Vector3.up, out hit))
     {
         if (outputNeuron[4].name == "Eat" && Sigmoid(outputNeuron[4].value) > 0.5f)
         {
             if (hit.transform.gameObject.tag == "Grass")
             {
                 Grass script = hit.transform.gameObject.GetComponent<Grass>();
                 Eat(true);
                 script.foodlevel -= 50;
             }
             else
             {
                 Eat(false);
             }
         }
     }
     if (outputNeuron[3].name == "Forward" && Sigmoid(outputNeuron[3].value) > 0.5f)
     {
         Forward(outputNeuron[3].value);
     }
     if (outputNeuron[2].name == "lRotate" && Sigmoid(outputNeuron[2].value) > 0.5f)
     {
         if (outputNeuron[1].name == "rRotate" && Sigmoid(outputNeuron[1].value) > 0.5f)
         {
             Debug.Log("NoRotation");
         }
         else
         {
             Rotate(0);
         }
     }
     if (outputNeuron[1].name == "rRotate" && Sigmoid(outputNeuron[1].value) > 0.5f)
     {
         if (outputNeuron[2].name == "lRotate" && Sigmoid(outputNeuron[2].value) > 0.5f)
         {
             Debug.Log("NoRotation");
         }
         else
         {
             Rotate(1);
         }
     }

 }
 
 public void Eat(bool food)
 {
     //Energy -= 10;
     if (food == true)
     {
         Energy += 50;
     }
 }

 public void Forward(float value)
 {
     this.transform.position += (this.transform.forward * value) / 4;
 }

 public void Birth()
 {
     if (Energy >= 50)
     {
         //GameManager manager = GameManager.GetComponent<GameManager>();
         GameObject child = Instantiate(this.gameObject, transform.position - new Vector3(1, 0, 0), Quaternion.identity);
         NeuronNetwork network = child.GetComponent<NeuronNetwork>();
         network.cooldowntimer = 30;
         for (int i = 0; i < network.inputConnection.Count; i++)
         {
             network.inputConnection[i].weight += UnityEngine.Random.Range(-0.1f, 0.1f);
         }
         for (int i = 0; i < network.outputConnection.Count; i++)
         {
             network.outputConnection[i].weight += UnityEngine.Random.Range(-0.1f, 0.1f);
         }
         //manager.birthes += 1;
         //Energy -= 30;

     }

     else
     {
         Energy -= 30;
     }
 }

 public void Rotate(int direction)
 {
     if (direction == 0)
     {
         transform.Rotate(new Vector3(0, -1, 0));
     }
     if (direction == 1)
     {
         transform.Rotate(new Vector3(0, 1, 0));
     }
 }

 public void Death()
 {
     GameManager manager = GameManager.GetComponent<GameManager>();
     if (Energy <= 0)
     {
         Destroy(this.gameObject);
         Destroy(this.transform);
         Destroy(this);
         manager.Creatures -= 1;
         manager.deaths += 1;
     }
 }

 public float Sigmoid(float value)
 {
     float et = (float)Math.Pow(Math.E, value);
     return et / (1 + et);
 }

}

Comment
Add comment · Show 1
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 whynot660 · Jan 16, 2017 at 09:13 AM 0
Share

the code is too long to read and analyze, you should give some explanation on how the energy is supposed to decrease what the object is doing ... you should tract the values change of Energy with Debug.Log("the instruction position of condition " + Energy); besides check your neuronal network is initialized with right values

0 Replies

· Add your reply
  • Sort: 

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

99 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

Related Questions

Problem with UNET: spawning instances like line renderes and bullets and make them show correctly on all clients 0 Answers

Need to press key 2 times to shoot 0 Answers

change color of instantiated object 1 Answer

Unity [PUN] New Instantiated players cannot see previously instantiated players 0 Answers

NullReferenceException problems. 0 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