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 BrentMichie · Oct 02, 2015 at 09:52 AM · c#scriptingbasicslinerenderer

Referencing a variable from another script

The problem I'm having is. I need to be able to generate Gameobjects with LineRenderers attached (or added to an empty Gameobject upon instantiation). I then want the newly instantiated LineRenderer object to reference some variables from the playerMover script which instantiated it.

I've seen examples where people use GameObject thePlayer = GameObject.Find("ThePlayer"); OR PlayerScript playerScript = thePlayer.GetComponent();

but my issue is that the script which instantiates the linerenderer object is used on multiple objects in my game and the tags for each of these objects are often the same "Offense" or "Defense" so I'm concerned that using Find or GetComponent will pickup the wrong 'parent' (don't think that's the right use of parent but its a similar relationship).

I want the playerMover script to instantiate a new gameobject with linerenderer and then pass it a few variables. The variables can be passed to the new game object immediately in void Start()

Can someone show me some code which will work for this.

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 hexagonius · Oct 02, 2015 at 10:04 AM

inject the info. after instantiation, grab the script from it in your playermover. create a public SetVariables method in there,or Init... whatever, and pass it either every info as a parameter, or create a data container which gets filled and passed. in your case, the indirection of waiting for Start and look for whoever instantiated is not necessary.

Comment
Add comment · Show 3 · 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 BrentMichie · Oct 02, 2015 at 08:30 PM 0
Share

I think this is the best solution. I have now encountered another problem with it.

I have declared public GameObject line; public LineCreator motionLR, playLR;

the following code executes in the main body and gives me an error:

motionLR = (LineCreator)Instantiate (line, Vector3.zero,Quaternion.identity);

motionLR.SetPosition (0, firstPos);
motionLR.SetPosition (1, secondPos);

In my LineCreator script I have a public function SetPosition() this piece is working.

The error I get is: "Cannot cast from source type to destination type"

I looked up the error and there is a range of different reasons why this might be happening. Am I not casting this properly?

Again, I just want to create a LineRenderer and give it a bunch of parameters which will draw a line on screen. It seems very simple yet this is giving me more problems than the rest of the project combined. Any thoughts are welcomed.

avatar image hexagonius BrentMichie · Oct 02, 2015 at 09:07 PM 0
Share

line is a gameobject, not a LineCreator. you need to run GetComponent on the instantiated gameobject. btw there is a generic version of instantiate available, no need for the cast at all.

avatar image BrentMichie hexagonius · Oct 03, 2015 at 12:53 AM 0
Share

How do I then modify: motionLR.SetPosition (0, firstPos); motionLR.SetPosition (1, secondPos);

Because it is telling me that SetPosition() is not a valid function on a gameobject. That makes sense its not. Can you give me some example code for how I could do this?

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

30 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

Related Questions

Interchangeable animations in a script? 0 Answers

Multiple Enemy Stats and Scripts 1 Answer

Should I inherit from Monobehaviour if the use of my C# script is only to hold script references? 2 Answers

Draw Line Between Distance Joint 2D and Position 0 Answers

Get Audio Source On Another Object 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