Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by KG3 · May 31, 2013 at 03:17 AM · prefabloadassignreferences

How can i assign prefab to transform without drag & drop from project to inspector

Hey Everyone I need help on assigning a prefab to either a variable or transform so, when I press space bar the prefab will show up on the screen. I am coding in C#, and I used the References.load() and it doesn't work I always get an need a cast Error. I do not know why using scripts in Javascript work with the documentation but in C# you always need something extra.

Here is my code:

 using UnityEngine;
 using System.Collections;
 
 public class player_script : MonoBehaviour {
     int health = 100;
     int speed = 5;
     GameObject collided_with;
     Transform laser_prefab;
     // Use this for initialization
     void Start () {
 
        GameObject instance = Instantiate(Resources.Load("Lazer"));
     
     }
     
     // Update is called once per frame
     void Update ()
     {
 
         if (Input.GetAxis("Horizontal") > 0 )
         {
             if (collided_with != null)
             {
                 if (collided_with.tag == "Left")
                 {
                     return;
                 }
             }
                 transform.Translate(new Vector3(1 * speed * Time.deltaTime, 0, 0));
             
 
         }
 
         if (Input.GetAxis("Horizontal") < 0 )
         {
             if (collided_with != null)
             {
                 if (collided_with.tag == "Right")
                 {
                     return;
                 }
             }
             transform.Translate(new Vector3(-1 * speed * Time.deltaTime, 0, 0));
 
 
         }
 
         if (laser_prefab != null)
         {
             if (Input.GetAxis("FireLazer") != 0)
             {
 
                 Instantiate(laser_prefab, new Vector3(transform.position.x, transform.position.y + 1), Quaternion.identity);
             }
         }
     }
 
 
     void OnCollisionEnter(Collision col)
     {
         collided_with = col.gameObject;
 
     }
     void OnCollisionExit(Collision col)
     {
         collided_with = null;
     }
 
 }

Here is the error I get from: GameObject instance = Instantiate(Resources.Load("Lazer"));

i says cannot convert type, your missing a type cast?, and it bothers me that the Documentation just translates the JS code to C# although not really go into the perspective of doing everything to make sure it works, like for example to use Vector3 You have to place new infront of it. Unity Documentation just uses the Vector3 without anything behind it.

but, anyways is there an easier way to assign prefabs and to make them show up, without dragging or dropping, I cannot seem to find the Transform, or my variable in the inspector, via it isn't showing up when I click on my script.

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
Best Answer

Answer by robertbu · May 31, 2013 at 04:06 AM

To fix the error change to:

 GameObject instance = Instantiate(Resources.Load("Lazer")) as GameObject;

As a true prefab, your approach is about as simple as it gets. Note Instantiate() will take a scene object not just a prefab. So if what you want to Instantiate() exists in the scene somewhere, you can use GameObject.Find() or GameObject.FindWithTag() to find a specific game object to instantiate.

Comment
Add comment · Show 1 · 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 KG3 · May 31, 2013 at 01:25 PM 0
Share

Thanks your the best, I'll Research more into those functions

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

13 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

Related Questions

Have some strange problem with Resources.Load 1 Answer

How to assign Transform to prefab ? 2 Answers

Create Transform from prefab file in a project 4 Answers

how to set custom parameters to GameObjects and access them? 1 Answer

Cannot drag and assign other class to prefab 1 Answer


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