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 Rizelyn · Jul 04, 2018 at 03:21 PM · instantiateparent

Instantiated object not receiving value from parent class

Hey,

I'm trying to make an instantiated object "enemy" move with the speed "moverEnemySpeed" set in the parent class. I don't get any errors indicating the child script can't find moverEnemySpeed, but when I Debug.Log it's value it says "0". In the parent script I set the value to 5...

Anyone know what I'm missing?

Parent class:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SpawnerController : MonoBehaviour 
 {
     //The Spawner game object manages the instantiation of the enemies 
     // Array of GameObject prefabs set in the Engine/Inspector
     public GameObject[] mode;
 
     // Spawntime settings
     public int spawnInterval;
     public float startWait;    
     
     public float moverEnemySpeed;
 
     
     // Runs on start-up game
     void Start () 
     {
         moverEnemySpeed = 5f;
         
         //Repeats a function on declared start-up time startWait and with set interval spawnInterval
         InvokeRepeating("Spawn",startWait, spawnInterval);    
         
         
     }
 
     //Spawns the enemies
     void Spawn () 
     {
 
         //Randomises a spawnposition along the x-axis
         Vector3 position = new Vector3(Random.Range(-3.0f,3.0f), 0, 8.0f);
         
         //By default the enemy planes are standing upright and invisible for the top down camera, rotating them 90 degrees on creation fixes that
         Quaternion spawnRotation = Quaternion.Euler(90,0,0); 
         
         //Picks a random enemy from the array and instantiates it in the game 
         (Instantiate(mode[Random.Range (0,mode.GetLength (0))],position, spawnRotation)  as GameObject).transform.parent = this.transform;
     }
     
 }

Child class:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Mover : SpawnerController 
 {
     //This script is applied to all instantiated enemy player to make them move downward the screen
     
     //Rigidbodies are needed to move the enemy
     private Rigidbody rb;
     
     // Runs on startup game
  void Start () 
     {
 
         rb = GetComponent<Rigidbody>(); //Gets the Rigidbody component
         
         rb.velocity = -transform.up * moverEnemySpeed; //Starts movement of enemies
         Debug.Log(moverEnemySpeed); 
     }
 
 }
 
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

2 Replies

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

Answer by tormentoarmagedoom · Jul 04, 2018 at 04:49 PM

Good day.

I dont see where you declare the mover enemy speed in the child script. How it knows what value needs to read?

You should try this :

 rb.velocity = transform.down * (transform.parent.GetComponent<SpawnerController>().moverEnemySpeed);

So it will use directly the value in the parents script.

Byee!

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

Answer by Rizelyn · Jul 04, 2018 at 07:51 PM

It worked, thanks!

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

107 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

Related Questions

Getting instance of an sub object rather than the original's subobject 0 Answers

Renaming Child Objects during Instantiate? 1 Answer

Instantiate Terrain Object as child of Empty Game Object 1 Answer

can't parent an Object, a second time. 0 Answers

Instantiate GameObject Parent targetting issue. 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