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 kIsForKid · Jul 17, 2018 at 06:15 PM · beginnernoobunity 4object referencecasting

Unable to create a reference to an instantiated object and alter it?

Hi, I'm a beginner to Unity and I'm trying to create a simple shooter with 4-directional movement.

When the player presses a direction on the arrow-pad, I want a bullet to spawn and move in that direction.

If the player presses the left arrow-key, then I want a bullet to spawn near the player and I want to simultaneously set the direction variable of the bullet to "left". In the Bullet class, the Update() method holds logic that moves the bullet in the direction specified in the direction variable.

However, I'm struggling with trying to create a reference to the bullet object and altering its variables.

PlayerController variables (the Bullet prefab is added to the bullet slot in the inspector):

 public class PlayerController : MonoBehaviour {
     public GameObject bullet;

In PlayerController Update() function:

 if (Input.GetKeyDown(KeyCode.LeftArrow)) {
             Bullet b = (Bullet) Instantiate(bullet, transform.position + Vector3.left, Quaternion.identity);
             b.SetDirection("left");
         }

Code for Bullet class:

 public class Bullet : MonoBehaviour {
 
     public string direction;
     private float bulletSpeed = 0.2f;
     private float speed = 10.0f;
     
     void Start () {
         direction = "right";
     }
     
     void Update () {
         if (direction == "left") transform.position += Vector3.left * speed * Time.deltaTime;
     }
     
     public void SetDirection(string bulletDirection) {
         direction = bulletDirection;
     }    
 }

The problem occurs when I press the left-arrow. I get the error for the line after if (Input.GetKeyDown):

InvalidCastException: Cannot cast from source type to destination type

I didn't think I needed the cast to Bullet because I thought Instantiate() would return the instance of Bullet that I just made, but when I left out the cast, I got this error for that same line:

Cannot implicitly convert type 'UnityEngine.Object' to 'Bullet'. An explicit conversion exists (are you missing a cast?)

And now, I'm lost. The Instantiate() method doesn't return a type Bullet, and if I try to cast it, I get an error. How can I grab that instance of the Bullet I created with that key press and then alter its direction?

Thanks in advance for any help. :)

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 bennett_apps · Aug 02, 2018 at 07:42 PM

Try saving the Instantiated object as GameObject, not Bullet. Then use GetComponent to grab the bullet script on the gameobject.

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

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

Raycast shotgun help! 2 Answers

UCE0001: ';' expected. Insert a semicolon at the end. 1 Answer

Really basic question... 1 Answer

How to make different game routes available depending on players previous choice? 0 Answers

Super beginner question on raycast 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